Does C++ 14 still generate default functions even if the class contains no data?
Clash 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?
c++ c++14
add a comment |Â
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?
c++ c++14
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
add a comment |Â
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?
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?
c++ c++14
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
add a comment |Â
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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