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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












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?







share|improve this question






















  • 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






  • 1




    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






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














up vote
6
down vote

favorite












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?







share|improve this question






















  • 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






  • 1




    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






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












up vote
6
down vote

favorite









up vote
6
down vote

favorite











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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








edited Sep 2 at 15:53

























asked Sep 2 at 15:21









Irfan

1714




1714











  • 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






  • 1




    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






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
















  • 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






  • 1




    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






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















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




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




1




1




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




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




3




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





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





3




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




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






active

oldest

votes

















up vote
7
down vote














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.






share|improve this answer






















  • 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






  • 2




    @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











Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52138353%2fdoes-c-14-still-generate-default-functions-even-if-the-class-contains-no-data%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
7
down vote














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.






share|improve this answer






















  • 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






  • 2




    @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















up vote
7
down vote














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.






share|improve this answer






















  • 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






  • 2




    @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













up vote
7
down vote










up vote
7
down vote










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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 2 at 15:37

























answered Sep 2 at 15:25









Rakete1111

31.9k973107




31.9k973107











  • 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






  • 2




    @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 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






  • 2




    @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 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




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




2




2




@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





@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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52138353%2fdoes-c-14-still-generate-default-functions-even-if-the-class-contains-no-data%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke