6

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?

Irfan
  • 214
  • 1
  • 4
  • You'll need to check the emitted code to verify this. There should be default functions generated, otherwise you'll have a problem when linking. – πάντα ῥεῖ Sep 02 '18 at 15:23
  • 1
    In this case the code has no observable effect, so the compiler is free to remove it from assembly completely. – zett42 Sep 02 '18 at 15:35
  • 3
    They will *conceptually* be generated, but your compilers optimizer will most likely throw them out on sight, since they do nothing. – Jesper Juhl Sep 02 '18 at 15:38
  • 3
    There is a substantial difference between generating *a function* and generating *code*. A function is a language-level entity that exists and has language-level meaning (e.g. can be called by other parts of a program, participates in overload resolution etc) regardless of whether it involves any non-empty executable code. – n. m. could be an AI Sep 02 '18 at 16:22

1 Answers1

7

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.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
  • Thanks for your time. I have started reading C++ material after a long time, and if I remember correctly, “odr-used” and “constant evaluated” are new terms for me. Can you please link them to a page where I would be able to find some information about what they stand for and what role they play. I would appreciate that. – Irfan Sep 02 '18 at 15:33
  • 2
    @Irfan There you go; those are links to the standard. If you want a better explanation: [odr-used](https://stackoverflow.com/questions/19630570/what-does-it-mean-to-odr-use-something). Couldn't find one for constant evaluated, but basically when it's used in a compile-time context (roughly). – Rakete1111 Sep 02 '18 at 15:39