Static claim to check static const class?

I have several classes with members of "static const". I would like to know how to check their values ​​at compile time using static_assert. Is it possible to put static_assert directly in the class body? (Putting my static_assert in each constructor is not very practical.)

+5
source share
1 answer

Yes, static_assert()you can place it anywhere an ad can appear. This includes the class body:

class C {
public:
    enum E {
      A, B, C,
      NumEes
    };
    constexpr Foo foos[] = { {...}, {...}, {...} };
    static_assert( NumEes == sizeof foos / sizeof *foos, "size mismatch" );

    // ...
};
+4
source

All Articles