Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有几个带有“静态常量”数据成员的类。我想知道如何在编译时使用 static_assert 检查它们的值。我可以将 static_assert 直接放在类体中吗?(将我的 static_assert 放在每个构造函数中并不是很实用。)
是static_assert()的,声明也可以放在任何地方。这包括一个类的主体:
static_assert()
class C { public: enum E { A, B, C, NumEes }; constexpr Foo foos[] = { {...}, {...}, {...} }; static_assert( NumEes == sizeof foos / sizeof *foos, "size mismatch" ); // ... };