Does C++ 14 still generate default functions even if the class contains no data?

Viewed 427

I am rereading Scott Meyers’ Effective C++ after a hiatus of 16 years. Although I have not read the latest C++ standard, but it has come to my attention that C++ has changed since the second edition of Effective C++ was written. In the third edition of his book, Scott Meyers has mentioned that even if you have an empty class, meaning there is nothing to initialize or assign, a C++ compiler will still generate at least 3 default functions, namely, default constructor, default copy constructor, assignment operator, and probably some other functions. According to Mr. Meyers, the following code will result in the generation of the aforementioned functions.

class Empty {}
Empty E1; // Default constructor.
Empty E2 ( E1 ); // Default copy constructor. 
E1 = E2; // Default assignment operator. 

Considering that there really is nothing to initialize, as the class is empty, does the C++ still generate some sort of code for the stated functions?

1 Answers

probably some other functions.

Yes, the move constructor and the move assignment operator. That's it.

Considering that there really is nothing to initialize, as the class is empty, does the C++ still generate some sort of code for the stated functions?

Sometimes. What happens is that those special members are declared, but not defined. They are only defined when they are used (i.e. odr-used or constant evaluated), otherwise, there is no code generated for them as they aren't defined.

And because you have an empty class, if the special members are defined, they will do nothing at all.

Related