gpt4 book ai didi

c++ - 可以在 constexpr 上下文中使用导致未指定(不是未定义!)行为的指针的表达式吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:48 28 4
gpt4 key购买 nike

根据 cppreference (强调我的):

A core constant expression is any expression that does not have any one of the following in any subexpression
(...)

  1. An expression whose evaluation leads to any form of core language undefined behavior (including signed integer overflow, division by zero, pointer arithmetic outside array bounds, etc). Whether standard library undefined behavior is detected is unspecified.

另一方面,有几个指针表达式的结果不是未定义而是未指定(参见 [expr.rel]/3 )例如:

struct A {
int v;
};

struct B {
int v;
};

struct C: A, B {} c;

int main() {
constexpr bool result = &c.A::v < &c.B::v;
(void)result;
}

代码编译没有问题 gcc但不在clang这无疑是真实的:

comparison of addresses of subobjects of different base classes has unspecified

但是(据我所知)根据 cppreference 它不应该阻止编译器编译代码。

哪个编译器就在这里 - gcc 还是 clang?我是否过度解读了 cppreference?

最佳答案

除了关于 UB 的包罗万象的情况之外,[expr.const] 中禁止表达式列表的末尾是,

— a relational or equality operator where the result is unspecified

这也出现在 cppreference 列表中,当前编号为 #19。

关于c++ - 可以在 constexpr 上下文中使用导致未指定(不是未定义!)行为的指针的表达式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42575122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com