通过指向成员的指针对嵌套访问进行编译时评估期间出现 GCC 错误
GCC error during compile-time evaluation of nested accesses via pointer to member
这是Clang接受的,但GCC不接受:
struct inner {int x;};
constexpr struct outer {inner i;} o{};
// definition of x4 results in GCC error:
// "accessing value of 'o' through a 'const int' glvalue in a constant expression"
constexpr int x1 { &(o.i)->*(&inner::x)}; // ok
constexpr int x2 { (&o->*(&outer::i)).x }; // ok
int x3 {&(&o->*(&outer::i))->*(&inner::x)}; // ok
constexpr int x4 {&(&o->*(&outer::i))->*(&inner::x)}; // error
GCC 9.4 错误消息是
error: accessing value of 'o' through a 'const int' glvalue in a constant expression
constexpr内部是否允许这种类型的访问?
在支持 C++11 的所有 GCC 版本上,该行为似乎相同。
由于代码被 Clang 和 MSVC 接受,显然它只是一个 GCC 错误,直到 GCC 9 才表现出来,并在 GCC 10 中得到修复,它也接受您的代码。演示:https://gcc.godbolt.org/z/3EdhaK9of
相关文章:
- 二叉排序树无法编译
- 在编译时评估函数开销的通用方法
- C++ constexpr语言 - 可以在编译时评估值吗?
- 使用 -O3 编译时的不同评估
- 是在编译时评估的C 文字中的算术操作
- 是否可以在编译时评估使用 std::is_same<> 的此条件?
- 通过指向成员的指针对嵌套访问进行编译时评估期间出现 GCC 错误
- 如何在编译时评估基元模板与类模板
- 编译时间评估函数以计算常数
- 在编译时评估 strlen?
- 概括编译时间和运行时间评估
- C 11:具有对数评估深度的编译时间阵列
- 编译时是否需要短路评估规则
- 是否可以在编译时评估数组
- 使用 #pragma message() 打印编译期间 #define 的完整评估结果
- 在C++中,是否可以在编译时使用模板元编程输出评估值
- C++循环的编译时评估
- 为什么编译器在编译期间不评估 constexpr 函数,当每个信息都给出时?
- 使用clang强制编译时对constexpr进行评估
- 文字类型成员的编译时评估