This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 112c. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-11-10


1188. Type punning in constant expressions

Section: 7.7  [expr.const]     Status: C++11     Submitter: Jason Merrill     Date: 2010-08-31

[Voted into the WP at the March, 2011 meeting as part of paper N3262.]

The status of the following example is not clear:

    union U { float f; unsigned long u; };

    constexpr U u1 = { 1.0 };
    constexpr unsigned long u2 = u1.u;

This might be ill-formed because the aliasing causes undefined behavior, which should make the expression not a constant expression. However, a similar example using a permitted aliasing would presumably be acceptable:

    union U {
        unsigned char c[sizeof(double)];
        double d;
    };
    constexpr U c1u = { 0x12, 0x34 /* etc. */ };
    constexpr double c1 = c1u.d;

One suggestion was that unions should not be considered literal types, but some in the ensuing discussion deemed that excessive. That also would not address similar examples using reinterpret_cast, which is currently also permitted in constant expressions.

Proposed resolution (November, 2010) [SUPERSEDED]:

Change 7.7 [expr.const] paragraph 2 as follows:

[Note: the proposed resolution of issue 1098 edits this bullet in an incompatible fashion.]