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

标签 c++ gcc c++11 clang constexpr

考虑以下代码:

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/

相关文章:

c++ - 无法将临时对象作为引用传递

c++ - W2CA 可能会丢失数据吧?

c++ - Qt中如何显示图片的颜色分量

C++ 部分排序因 2 个模板参数而失败

c++ - 即使使用 -Wall 标志,gcc 也不会发出警告

c++ - 可变参数模板的未声明标识符

iphone - 为什么这个C++(cocos2d,box2d)例子给出的是指针而不是值?

c++ - 使用模板确定 std::array 大小 CRTP

c - 具有恒定大小的数组(全局与堆栈)

lambda - 我应该将 lambda 设为静态吗?