gpt4 book ai didi

c++ - constexpr 和带有重新解释转换的静态 const void 指针的初始化,哪个编译器是正确的?

转载 作者:行者123 更新时间:2023-12-01 14:29:53 35 4
gpt4 key购买 nike

考虑以下代码:

struct foo {
static constexpr const void* ptr = reinterpret_cast<const void*>(0x1);
};

auto main() -> int {
return 0;
}

以上示例在 g++ v4.9 ( Live Demo ) 中编译良好,但在 clang v3.4 ( Live Demo ) 中编译失败并生成以下错误:

error: constexpr variable 'ptr' must be initialized by a constant expression

问题:

  • 根据标准,这两种编译器哪个是正确的?

  • 声明这种表达式的正确方法是什么?

最佳答案

长话短说

clang 是正确的,这是已知的 gcc 错误。您可以改用 intptr_t 并在需要使用该值时进行转换,或者如果这不可行,则 gccclang 都支持一点应允许您的特定用例的已记录变通办法。

详情

因此,如果我们转到 draft C++11 standard,那么 clang 在这一点上是正确的5.19 常量表达式2 说:

A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression [...]

并包括以下项目符号:

— a reinterpret_cast (5.2.10);

一个简单的解决方案是使用 intptr_t :

static constexpr intptr_t ptr = 0x1;

然后在您需要使用它时进行转换:

reinterpret_cast<void*>(foo::ptr) ;

可能很想就此打住,但这个故事会变得更有趣。这是已知且仍然打开的 gcc 错误,请参阅 Bug 49171: [C++0x][constexpr] Constant expressions support reinterpret_cast .从讨论中可以清楚地看出,gcc 开发人员对此有一些明确的用例:

I believe I found a conforming usage of reinterpret_cast in constant expressions useable in C++03:

//---------------- struct X {  X* operator&(); };

X x[2];

const bool p = (reinterpret_cast<X*>(&reinterpret_cast<char&>(x[1]))
- reinterpret_cast<X*>(&reinterpret_cast<char&>(x[0]))) == sizeof(X);

enum E { e = p }; // e should have a value equal to 1
//----------------

Basically this program demonstrates the technique, the C++11 library function addressof is based on and thus excluding reinterpret_cast unconditionally from constant expressions in the core language would render this useful program invalid and would make it impossible to declare addressof as a constexpr function.

但无法获得针对这些用例的异常(exception)情况,请参阅 closed issues 1384 :

Although reinterpret_cast was permitted in address constant expressions in C++03, this restriction has been implemented in some compilers and has not proved to break significant amounts of code. CWG deemed that the complications of dealing with pointers whose tpes changed (pointer arithmetic and dereference could not be permitted on such pointers) outweighed the possible utility of relaxing the current restriction.

但是 显然 gccclang 支持一个小的文档扩展,允许使用 __builtin_constant_p (exp) 对非常量表达式进行常量折叠因此,gccclang 都接受以下表达式:

static constexpr const void* ptr = 
__builtin_constant_p( reinterpret_cast<const void*>(0x1) ) ?
reinterpret_cast<const void*>(0x1) : reinterpret_cast<const void*>(0x1) ;

为此查找文档几乎是不可能的,但是这 llvm commit is informative以下片段提供了一些有趣的阅读:

support the gcc __builtin_constant_p() ? ... : ... folding hack in C++11

和:

// __builtin_constant_p ? : is magical, and is always a potential constant.

和:

// This macro forces its argument to be constant-folded, even if it's not
// otherwise a constant expression.
#define fold(x) (__builtin_constant_p(x) ? (x) : (x))

我们可以在 gcc-patches 电子邮件中找到关于此功能的更正式的解释:C constant expressions, VLAs etc. fixes其中说:

Furthermore, the rules for __builtin_constant_p calls as conditional expression condition in the implementation are more relaxed than those in the formal model: the selected half of the conditional expression is fully folded without regard to whether it is formally a constant expression, since __builtin_constant_p tests a fully folded argument itself.

关于c++ - constexpr 和带有重新解释转换的静态 const void 指针的初始化,哪个编译器是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48212565/

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