[PATCH] Add __builtin_addressof
richard at metafoo.co.uk
Wed Jul 10 19:26:21 PDT 2013
On Wed, Jul 10, 2013 at 7:08 PM, Eli Friedman <eli.friedman at gmail.com> wrote:
> Forgot to add cfe-commits.
> On Wed, Jul 10, 2013 at 7:07 PM, Eli Friedman <eli.friedman at gmail.com> wrote:
>> On Wed, Jul 10, 2013 at 5:40 PM, Richard Smith <richard at metafoo.co.uk> wrote:
>>> The attached patch adds a __builtin_addressof, that performs the same
>>> functionality as the built-in & operator (ignoring any overloaded
>>> operator& for the type). The purpose of this builtin is for use in
>>> std::addressof, to allow it to be made constexpr; the existing
>>> implementation technique (reinterpret_cast to some reference type,
>>> take address, reinterpert_cast back) does not permit this because
>>> reinterpret_cast between reference types is not permitted in a
>>> constant expression in C++11 onwards.
>> Not really an objection, but why do we disallow reinterpret_casts in
>> constant expressions in the first place? It seems like it would be
>> convenient functionality to have.
One goal of this restriction is to prohibit a constant expression from
accessing the object representation of a value; there's no other rule
which would obviously prohibit:
constexpr int *p = &n;
constexpr char unpossible = *reinterpret_cast<char*>(&p);
Also, pointers-reinterpret_cast-to-integers would be very problematic,
since we'd somehow still have to track the rich pointer representation
in case the integer is cast back to a pointer -- and even that
requires us to make a number of assumptions about the C++ object model
that haven't ever been broadly agreed. And so on.
Ultimately, IIRC, CWG thought that all you would be able to do with
the result of a reinterpret_cast would be to directly use it in the
value of an object, or to reinterpret_cast it back to the original
type. In the former case, you can store the value prior to the cast
instead, and in the latter case, you can remove the reinterpret_casts
instead (except for corner cases such as the implementation of
std::addressof, which we agreed could be handled by some
implementation-dependent mechanism). CWG thought that it would be
better to ban reinterpret_cast altogether rather than having it
produce a very special value that can only be used in highly
>> Minor review comment: please commit moving CheckAddressOfOperand onto
>> Sema in a separate patch.
Will do, thanks.
More information about the cfe-commits