Bug 56215 - Cannot create constexpr struct with unnamed unions
Summary: Cannot create constexpr struct with unnamed unions
Status: RESOLVED DUPLICATE of bug 54922
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-02-06 04:04 UTC by Takaki Makino
Modified: 2013-02-06 09:34 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Takaki Makino 2013-02-06 04:04:25 UTC
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);
------
Comment 1 Paolo Carlini 2013-02-06 09:34:07 UTC
Dup.

*** This bug has been marked as a duplicate of bug 54922 ***