enum PieceType
{
    NoPieceType, Pawn, Knight, Bishop, Rook, Queen, King,
    AllPieces = 0,
    PieceType_N = 8
};

template<PieceType T> Score OutpostEvaluator()
{
    static_assert(T == Bishop || T == Knight); // Doesn't compile.....
}

我想确保模板函数只能用于某些类型的枚举值,在此例中为Bishop和Knight。 std::is_scalar(),std::is_enum()和其他类型支持在我的情况下似乎不起作用。我该如何实现?

最佳答案

http://en.cppreference.com/w/cpp/language/static_assert的语法,来自ojit_a。

static_assert ( bool_constexpr , message );

您需要提供一条消息。就像是:
static_assert(T == Bishop || T == Knight, "Expected Bishop or Knight");

10-01 16:07