这些成员变量有什么区别:
struct my_class {
static const int i = 0;
static constexpr int j = 0;
};
如果我的理解是正确的,我可以同时使用 i
和 j
作为编译时间常数。也就是说,std::array<int, my_class::i>
和 std::array<int,my_class::j>
应该可以。
最佳答案
整数或枚举类型的成员没有区别(如您的示例)。对于所有其他类型,常量表达式 require constexpr
:
an lvalue-to-rvalue conversion (4.1) unless it is applied to
- a non-volatile glvalue of integral or enumeration type that refers to a complete non-volatile const object with a preceding initialization, initialized with a constant expression, or […]
- a non-volatile glvalue that refers to a non-volatile object defined with
constexpr
, or that refers to a non-mutable sub-object of such an object, or […]
关于c++ - C++11/14 中的静态常量与 constexpr 成员字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36282162/