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.)
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" );
    // ...
};
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With