Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++14

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?

like image 664
Irfan Avatar asked Sep 02 '18 15:09

Irfan


People also ask

Does every class have a default constructor C++?

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.

What are the default functions provided in C++ class?

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.

What does default mean 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.

What are default functions in a class?

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.


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.

like image 172
Rakete1111 Avatar answered Sep 19 '22 20:09

Rakete1111