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

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



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?





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 2 at 15:23





In this case the code has no observable effect, so the compiler is free to remove it from assembly completely.
– zett42
Sep 2 at 15:35





They will conceptually be generated, but your compilers optimizer will most likely throw them out on sight, since they do nothing.
– Jesper Juhl
Sep 2 at 15:38






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.
Sep 2 at 16:22




1 Answer
1



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.





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 2 at 15:33





@Irfan There you go; those are links to the standard. If you want a better explanation: odr-used. Couldn't find one for constant evaluated, but basically when it's used in a compile-time context (roughly).
– Rakete1111
Sep 2 at 15:39




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)