Do unrestricted unions require placement new and a constructor definition?

前端 未结 2 941
借酒劲吻你
借酒劲吻你 2020-12-11 04:25

The examples I\'ve seen of unrestricted unions always seem to use placement new when constructing. The Wikipedia article for C++11 features uses placement new in the constru

相关标签:
2条回答
  • 2020-12-11 04:51

    No, placement new is not required here. The standard says that in case of unrestricted unions, the field constructors should be called explicitly, otherwise the fields will be uninitialized.

    You can call the constructor using traditional way

    U(): p() {}
    

    and exotic way

    U() { new(&p) Point(); }
    

    The second variant may be useful if it is not possible to construct the field before calling the constructor of U.

    Don't also forget about placement destructors in this case.

    0 讨论(0)
  • 2020-12-11 05:05

    Introduction

    The snippet you have shown is perfectly safe; you are legally allowed to initialize one non-static data-member when initializing your union-like class.


    Example

    The wikipedia article has an example where placement-new is used because they are writing to a member after the point in which it is possible to directly initialize a certain member.

    union A {
       A () { new (&s1) std::string ("hello world"); }
      ~A () { s1.~basic_string<char> (); }
      int         n1;
      std::string s1;
    };
    

    The previous snippet is however semantically equivalent to the following, where we explicitly state that A::s1 shall be initialized when constructing A.

    union A {
       A () : s1 ("hello world") { }
      ~A () { s1.~basic_string<char> (); }
      int         n1;
      std::string s1;
    };
    

    Elaboration

    In your snippet you have an anonymous union inside your class (which makes your class a union-like class), which means that unless you initialize one of the members of the union during initialization of the class — you must use placement-new to initialize them at a later time.


    What does the Standard say?

    9.5/1 -- Unions -- [class.union]p1

    In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time.

    3.8/1 -- Object lifetime -- [basic.life]p1

    [...]

    The lifetime of an object of type T begins when:

    • storage with the proper alignment and size for type T is obtained, and
    • if the object has non-trivial initialization, its initialization is complete.

    The lifetime of an object of type T ends when:

    • if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or
    • the storage which the object occupies is reused or released.
    0 讨论(0)
提交回复
热议问题