TITLE: trivial constructor (Newsgroups: comp.std.c++, 24 Aug 96) PROBLEM: <321c7398.3367286@smtp.ma.ultranet.com>, Pablo Halpern In the April 1995 DWP, in the section about unions [class.union], members of a union are restricted as follows: ... An object of a class with a non-trivial default construc- tor (_class.ctor_), a non-trivial copy constructor (_class.copy_), a non-trivial destructor (_class.dtor_), or a non-trivial copy assign- ment operator (_over.ass_, _class.copy_) cannot be a member of a union, nor can array of such objects. In the secton about constructors [class.ctor], a "trivial" default constructor is defined as follows: [ snip ] For example, the following class cannot be used in a union because it has no default constructor: class complex { double real, imaginary; public: complex(double re, double im = 0.0); // ... }; If we add a default constructor: complex() { } it still cannot be used in a union because the default constructor is not trivial. Note that this problem *does not* occure for the copy constructor because *every class* has a copy constructor and it is never necessary to declare one yourself if the implicitly defined one will suffice. Similarly, it is not a problem for the destructor. My proposed solution: Change the wording of the definition of a trivial default constructor to something like the following (I tried to make this less verbose, but failed): An *empty* constructor is one that is inline, has no parameters (not even default parameters), no explicit member initialization list, and no statements or declarations within the constructor body. An empty constructor for class X would therefore be defined: class X { X() { } }; A default constructor is trivial if it is an implicitly-declared default constructor or a public empty constructor and if: [ bullet list the same as in existing text (above) ] RESPONSE: clamage@pacific-88.Eng.Sun.COM (Steve Clamage) It's not a terrible idea and it doesn't seem to have much impact on the language or the rest of the standard and it doesn't seem difficult to implement. If it had been proposed a while back (for example, during the last public comment period in May 1995) it might have been considered. At this point, however, the C++ committee is interested only in fixing obvious errors, and things which have a major negative impact on a broad range of programs. It is also helpful to review how the definition of "trivial" constructors evolved. The ARM talks about classes which have "no constructor" in an effort to describe compatibility with C struct features. But of course, every class has at least a copy constructor, and every C-style struct also has a (trivial) default constructor. The intention was that C code like a union containing structs would continue to be valid. With the present wording, structs are allowed in unions if their construction and destruction have no impact, loosely speaking -- their constructors and destructors don't do anything, so it doesn't matter whether they get executed. With a union, the usual guarantees about construction and destruction cannot be made. If a class has a user-defined constructor or destructor, we must assume that construction or destruction semantics do matter, and so that class should not be part of a union. The way to say that construction and destruction semantics don't matter is to provide no constructor or destructor. In your example, you could change the constructor to an ordinary named member function. If that function requires features unique to constructors (like a member-init-list) so that you can't make such a change, then construction semantics do matter and the class should not be part of a union. The case where you are not allowed to change the class but you also need to put it in a union I would think would be unusual enough so as not to justify a delay in completing work on the C++ standard.