c++ - 基类的 constexpr 地址

标签 c++ base-class constexpr c++14

用clang 3.4(trunk),有什么方法可以用常量表达式计算基类的位移吗?

struct A { int a; };
struct B { int b; };

struct C: A, B {};

// cannot access base class of null pointer:
constexpr auto c_b_address = /*(size_t)*/ &(B&) *(C*)nullptr; 

最佳答案

是的,可以用常量表达式计算基类的位移,但它根本不可移植。你可以使用一个鲜为人知但documented clang 也支持的 gcc 扩展。当与运算符 ?::

一起使用时,它涉及到 __builtin_constant_p 的使用
#define CB (&(B&)*(C*)nullptr)
constexpr auto c_b_address = __builtin_constant_p CB ? CB : CB;

请注意,我刚刚使用宏 CB 来说明发生了什么。当然,这也可以通过多次重复表达式来完成。顺便说一句,我第一次知道这个技巧in this question其中包含有用的背景信息。

正如您可能已经了解的那样,基本问题是 constexpr 表达式中不允许使用 reinterpret_cast 或等效的 C 风格转换。奇怪的是,C 风格的转换(如上)被接受,但 reinterpret_cast(不会生成任何代码)不被接受。我还尝试了晦涩但看似合适的 ->* 运算符,但结果喜忧参半:

#define CB1 (&(B&)*(C*)nullptr)
#define CB2 (&((reinterpret_cast<C*>(nullptr))->*(&C::b)))
#define CB3 (&(((C*)(nullptr))->*(&C::b)))
#define CB4 (&(B&)*reinterpret_cast<C*>(nullptr))
#define CB CB1

g++ 4.8.3 和 clang++ 3.4 的结果:

     g++             clang++
--- ------------    --------
CB1  OK              OK
CB2  compile error   compile error
CB3  OK              compiles but gives answer = 0
CB4  compile error   compile error

在我运行 Linux 的 64 位机器上,只有 CB1 在两个编译器中都产生了正确答案 4。对于 gcc,CB1CB2 都可以使用或不使用 __builtin_constant_p。使用 clang,唯一有效的版本是 CB1 with __builtin_constant_p

为什么我们应该相信这是故意的行为?

正如@ShafikYaghmour 在评论中非常合理地问道,“您是否有 gcc 或 clang 引用资料表明它们支持这种行为?”我将扩大这个问题以询问“存在哪些文档表明这是有意的,而不仅仅是奇怪的副作用?”毕竟,如果有人真的要使用它,最好有一些迹象表明它可能在未来继续存在。本节试图解决这个问题。

对于clang,引用是the source code itself其中 VisitConditionalOperator 函数中的注释说:

// If the condition (ignoring parens) is a __builtin_constant_p call,
// the result is a constant expression if it can be folded without
// side-effects. This is an important GNU extension. See GCC PR38377
// for discussion.

这又指向 gcc 的 Bugzilla bug 38377其中讨论了这个问题。具体来说,在 2008 年,这个错误被报告为“__builtin_constant_p(t) ? t : 1 不被认为是常量整数表达式”。在讨论中,注意到对于条件运算符 (?:),

Yes, this is a (documented) special case required to be compatible with existing GNU C code.

此外,

If you get this wrong for C then GCC will fail to bootstrap, as it's part of the GNU C semantics GCC relies on when being built with GCC.

鉴于此,该行为似乎既具体又经过深思熟虑,并且由于 gcc 本身依赖于它,因此可能是相当稳定的行为。

不过,所有关于使用非标准实现细节的常见警告仍然适用。如果您可以在运行时执行此操作,那么 gcc 和 clang 都可以接受它:

ptrdiff_t cb = (char *)(&(B&)*(C*)nullptr) - (char *)nullptr;

关于c++ - 基类的 constexpr 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215720/

相关文章:

c++ - 我解决了一个问题,不知道背后的原因

c++ - 模板方法覆盖非模板方法

c# - 重铸为派生类型

c++ - std::max 在统一初始化和 constexpr int 下表现出乎意料

c++ - 在 Qt 中移动 Snake 时遇到问题

c++ - 什么时候使用 const string& 与 const string 比较好

C++继承问题

c++ - 模板类和基类?

c++ - 在 constexpr 变量模板中模拟 nan/inf

c++ - 为什么 std::array::size constexpr 具有简单类型(int、double、...)而不是 std::vector (GCC)?