【发布时间】:2020-05-03 07:41:31
【问题描述】:
如何从结构中正确访问字符串“bye”?
#include <iostream>
static constexpr char hi[] = "hi";
struct S{ static constexpr char bye[] = "bye"; };
int main(int argc, char *argv[]) {
typedef struct S ST;
std::cout << hi << std::endl; // this prints "hi"
std::cout << sizeof(ST::bye) << std::endl; // this correctly prints 4
std::cout << ST::bye << std::endl; // this does not compile with "undefined reference"
}
我正在使用一个 c++ 框架,该框架具有这种格式的一些配置(甚至在多重嵌套结构中),以使其值在编译时可用。我对 C++ 的了解不够深入,无法掌握这里的根本问题。我也不能争论为什么选择这种实现配置的方法并且不能改变它。
【问题讨论】:
-
用c++17编译
-
这能回答你的问题吗? constexpr static member before/after C++17
-
使用
::操作符访问类的静态成员,如S::bye -
typedef struct S ST;->using ST = S;
标签: c++ arrays string constexpr