http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56215

             Bug #: 56215
           Summary: Cannot create constexpr struct with unnamed unions
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: t-gcc-bugzi...@snowelm.com


Similar to PR c++/51675, but the trunk GCC fails to compile the following code:
------
struct foo {
    union
    {
        int x;
        short y;
    };
    constexpr foo() : x( 0 ) { }
};
------

$ g++ --std=c++11 hoge.cpp
hoge.cpp: In constructor ‘constexpr foo::foo()’:
hoge.cpp:7:33: error: uninitialized member ‘foo::<anonymous>’ in ‘constexpr’
constructor
      constexpr foo() : x( 0 ) { }
                             ^

What I actually want to do is a specialization of std::pair to provide
efficient comparison.  Since this requires an unnamed struct within an unnamed
union, and
the bug seems very sensitive to details like that, I put the code too.
GCC trunk also fails compiling the following.

------
#include <utility>
#include <cstdint>

namespace std {
template <>
struct pair< uint32_t, uint32_t > {
        union
        {
            struct {
                uint32_t first;
                uint32_t second;
            };
        uint64_t paired;
    };
    constexpr pair() : paired( ) { }
    constexpr pair(uint32_t a, uint32_t b) : first(a), second(b) { }
};
constexpr bool operator==
    ( pair<uint32_t,uint32_t> a, pair<uint32_t,uint32_t> b )
{
    return a.paired == b.paired;
}
constexpr bool operator<
    ( pair<uint32_t,uint32_t> a, pair<uint32_t,uint32_t> b )
{ // only for little endian
    return ((a.paired<<32)|(a.paired>>32)) < ((b.paired<<32)|(b.paired>>32));
}
} // namespace

std::pair<uint32_t,uint32_t> x(1, 2);
------

Reply via email to