Reputation: 241
for example,
enum tr {z, u};
template<tr T>
class test
{
assert(T is z or u);//how can I add assertions in this field?
};
in the comment how can I add code to assert the template T should only be z or u?
Upvotes: 0
Views: 316
Reputation: 16354
This can be done using static_assert
as @πάντα-ῥεῖ already pointed out:
enum tr {z, u, bar};
template<tr T>
class test
{
static_assert(T==z||T==u, "T must be z or u");
};
int main()
{
test<z> t_valid;
test<bar> t_fails; // compilation fails
return 0;
}
Upvotes: 2