Reputation: 60441
I have several classes with "static const" data members. I would like to know how to check their values at compile time with static_assert. Can I put static_assert directly in the class body ? (Putting my static_assert in every constructor is not very practical.)
Upvotes: 5
Views: 981
Reputation: 25313
Yes, static_assert()
can be placed everywhere a declaration can, too. That includes the body of a class:
class C {
public:
enum E {
A, B, C,
NumEes
};
constexpr Foo foos[] = { {...}, {...}, {...} };
static_assert( NumEes == sizeof foos / sizeof *foos, "size mismatch" );
// ...
};
Upvotes: 4