Member of Union has User-Defined Constructor

旧巷老猫 提交于 2019-12-23 02:24:07

问题


For the following code:

class Foo{
    int foo;
public:
    Foo() : foo(13) {}
    int getFoo() const { return foo; }
};

union Bar{
    Foo fBar;
    double dBar;
};

I believe this is fully legal in C++. http://en.cppreference.com/w/cpp/language/union#Explanation says:

If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler

And thus in gcc I can do this:

Bar bar = { Foo() }

When I try this in Visual Studio 2008 I get the error:

error C2620: member Bar::fBar of union Bar has user-defined constructor or non-trivial default constructor

Error C2620 states:

A union member cannot have a default constructor.

What's going on here? Was this ever a C++ requirement, I thought that Standard Layout was the only requirement? Is there a work around for this?


回答1:


In C++98/03 the C++ standard stated in 9.5

[...]If a POD-union contains several POD-structs that share a common initial sequence (9.2), and if an object of this POD-union type contains one of the POD-structs, it is permitted to inspect the common initial sequence of any of POD-struct members;[...]

And this was changed in C++11 to

[...]If a standard-layout union contains several standard-layout structs that share a common initial sequence (9.2), and if an object of this standard-layout union type contains one of the standard-layout structs, it is permitted to inspect the common initial sequence of any of standard-layout struct members;[...]

So before C++11 you could only use a POD type inside a union which means MSVS 2008 is giving you the right error. In order to use the new type of union you need to get the version of MSVS that supports that change. From this MSDN article we can see under the section for Unrestricted unions that that change was not made until version 2015.

You are either going to have to upgrade or change the class to be a POD type



来源:https://stackoverflow.com/questions/40088319/member-of-union-has-user-defined-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!