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?
For example, all members of class type, and their class-type members, must have a default constructor and destructors that are accessible. All data members of reference type and all const members must have a default member initializer.
In C++, the compiler automatically generates the default constructor, copy constructor, copy-assignment operator, and destructor for a type if it does not declare its own. These functions are known as the special member functions, and they are what make simple user-defined types in C++ behave like structures do in C.
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.
If your class does not define one or more of the special member functions, then the compiler may implicitly declare and define the functions that are used. The compiler-generated implementations are called the default special member functions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With