Is it possible to put several objects together inside a union?

后端 未结 6 1929
无人及你
无人及你 2021-02-08 06:38

What if I have this:

union{
    vector intVec ;
    vector floatVec ;
    vector doubleVec ;
} ;

Of cours

6条回答
  •  执念已碎
    2021-02-08 07:05

    Current C++ standard does not allow non-POD types inside unions. You will get this compiler error from gcc:

    error: member ‘std::vector >
    ::i’ with constructor not allowed in union
    error: member ‘std::vector >
    ::i’ with destructor not allowed in union
    

    New C++ standard (C++0x) proposes unrestricted unions, but it adds yet more object lifetime pitfalls to C++.

提交回复
热议问题