Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static assert to check static const class data members?

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.)

like image 805
Vincent Avatar asked Oct 08 '22 12:10

Vincent


1 Answers

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" );

    // ...
};
like image 66
Marc Mutz - mmutz Avatar answered Oct 13 '22 12:10

Marc Mutz - mmutz