c++ - C++11/14 中的静态常量与 constexpr 成员字段?

标签 c++ templates c++11 c++14 constexpr

这些成员变量有什么区别:

struct my_class {
    static const int i = 0;
    static constexpr int j = 0;
};

如果我的理解是正确的,我可以同时使用 ij作为编译时间常数。也就是说,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/

相关文章:

c++ - CMake 无法正确地将 ultralight ui 库链接到 Clion 2019.1.2 中的可执行文件

c++ - makefile 中的 CPP_LINK 是什么?

php - C++ SOAP 客户端

c++ - C++11 : is there a simple way to seed the generator in one place of the code, 中的随机数然后在不同的函数中使用它?

c++ - 如何从 OpenSSL 中获取随机盐作为 std::string

c++ - 字典顺序的定义?

c++ - "template <class T, class C>"和 "template <class T> template <class C>"有什么区别

javascript - 如何在用户控制时禁用 <a onclick>

c++ - 如何在模板中返回 lambda 指针?

不使用静态存储的基于 C++ 类型的缓存