简体   繁体   English

静态断言检查静态const类数据成员?

[英]Static assert to check static const class data members?

I have several classes with "static const" data members. 我有几个带有“static const”数据成员的类。 I would like to know how to check their values at compile time with static_assert. 我想知道如何使用static_assert在编译时检查它们的值。 Can I put static_assert directly in the class body ? 我可以将static_assert直接放在类体中吗? (Putting my static_assert in every constructor is not very practical.) (将static_assert放在每个构造函数中都不太实用。)

Yes, static_assert() can be placed everywhere a declaration can, too. 是的, static_assert()也可以放在声明的任何地方。 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" );

    // ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM