Why can't I use =default for default ctors with a member initializer listWhy can't variables be declared in a switch statement?C++11 allows in-class initialization of non-static and non-const members. What changed?defaulted ctor differences between gcc 4.6 and 4.7Default, value and zero initialization messDefault initialization of class data members in C++11MSVC2015 initializes class member that should be left uninitializedCould a class type with deleted default constructor be default initialized?Enum Class Default InitializationWhy is a member not getting zero-initialized in this example?std::vector not initialized by default ctor
Can there be an UN resolution to remove a country from the UNSC?
What is "industrial ethernet"?
Swapping rooks in a 4x4 board
What exactly is the 'online' in OLAP and OLTP?
How to model a twisted cylinder like this
Unusual mail headers, evidence of an attempted attack. Have I been pwned?
Is it illegal to withhold someone's passport and green card in California?
How would modern naval warfare have to have developed differently for battleships to still be relevant in the 21st century?
What does the hyphen "-" mean in "tar xzf -"?
Has there been any indication at all that further negotiation between the UK and EU is possible?
Why did pressing the joystick button spit out keypresses?
Parameterize chained calls to a utility program in Bash
How large would a mega structure have to be to host 1 billion people indefinitely?
Why don't countries like Japan just print more money?
How many people are necessary to maintain modern civilisation?
If I wouldn't want to read the story, is writing it still a good idea?
What is the legal status of travelling with methadone in your carry-on?
NSE Numerical IQ Test no.12: 759802, 358829, 847123,?
Why do some professors with PhDs leave their professorships to teach high school?
Why do all the teams that I have worked with always finish a sprint without completion of all the stories?
What happens to Cessna electric flaps that are moving when power is lost?
What can I do with a research project that is my university’s intellectual property?
Drawing people along with x and y axis
Why use cross notes in sheet music for hip hop tracks?
Why can't I use =default for default ctors with a member initializer list
Why can't variables be declared in a switch statement?C++11 allows in-class initialization of non-static and non-const members. What changed?defaulted ctor differences between gcc 4.6 and 4.7Default, value and zero initialization messDefault initialization of class data members in C++11MSVC2015 initializes class member that should be left uninitializedCould a class type with deleted default constructor be default initialized?Enum Class Default InitializationWhy is a member not getting zero-initialized in this example?std::vector not initialized by default ctor
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Consider the following class:
class Foo
int a, b;
public:
Foo() : a1, b2 // Default ctor with member initializer list
//Foo() : a1, b2 = default; // Does not work but why?
;
(Edit: because it was mentioned in a couple of answers - I'm aware of in-class member initializiers, but that's not the point here)
I think the second ctor definition would be more elegant and fit better into modern C++ code (see also why you should use =default
if you have to be explicit about using the default semantics). However, no common compiler seems to accept it. And cppreference is silent about it.
My first thought was that a member initializer list in a way changes the "default semantics" as explained in the linked FAQ, because it may or may not default-construct members. But then we would have the same problem for in-class initializers, just that here Foo() = default;
works just fine.
So, why is it disallowed?
c++ c++11 language-lawyer default-constructor ctor-initializer
|
show 5 more comments
Consider the following class:
class Foo
int a, b;
public:
Foo() : a1, b2 // Default ctor with member initializer list
//Foo() : a1, b2 = default; // Does not work but why?
;
(Edit: because it was mentioned in a couple of answers - I'm aware of in-class member initializiers, but that's not the point here)
I think the second ctor definition would be more elegant and fit better into modern C++ code (see also why you should use =default
if you have to be explicit about using the default semantics). However, no common compiler seems to accept it. And cppreference is silent about it.
My first thought was that a member initializer list in a way changes the "default semantics" as explained in the linked FAQ, because it may or may not default-construct members. But then we would have the same problem for in-class initializers, just that here Foo() = default;
works just fine.
So, why is it disallowed?
c++ c++11 language-lawyer default-constructor ctor-initializer
5
As soon as you've defined initialisers in the ctor you've provided a ctor and it's no longer the default.
– Jonathan Potter
Jun 5 at 12:38
2
Because "Default ctor with member initializer list" is not a default ctor. You can use inline initialization with "=default" though.
– Steve
Jun 5 at 12:38
1
@Quimby that's not an argument, since this is always the case. Also that's not the reason for=default
, see also the referenced C++ FAQ
– andreee
Jun 5 at 12:40
2
Could you explain what you would expect it to mean?
– molbdnilo
Jun 5 at 12:42
2
Indeed,and
=default
accomplish different things.is generally not what you want as it kills implicitly generated default move constructor/operator.
– ssteinberg
Jun 5 at 12:45
|
show 5 more comments
Consider the following class:
class Foo
int a, b;
public:
Foo() : a1, b2 // Default ctor with member initializer list
//Foo() : a1, b2 = default; // Does not work but why?
;
(Edit: because it was mentioned in a couple of answers - I'm aware of in-class member initializiers, but that's not the point here)
I think the second ctor definition would be more elegant and fit better into modern C++ code (see also why you should use =default
if you have to be explicit about using the default semantics). However, no common compiler seems to accept it. And cppreference is silent about it.
My first thought was that a member initializer list in a way changes the "default semantics" as explained in the linked FAQ, because it may or may not default-construct members. But then we would have the same problem for in-class initializers, just that here Foo() = default;
works just fine.
So, why is it disallowed?
c++ c++11 language-lawyer default-constructor ctor-initializer
Consider the following class:
class Foo
int a, b;
public:
Foo() : a1, b2 // Default ctor with member initializer list
//Foo() : a1, b2 = default; // Does not work but why?
;
(Edit: because it was mentioned in a couple of answers - I'm aware of in-class member initializiers, but that's not the point here)
I think the second ctor definition would be more elegant and fit better into modern C++ code (see also why you should use =default
if you have to be explicit about using the default semantics). However, no common compiler seems to accept it. And cppreference is silent about it.
My first thought was that a member initializer list in a way changes the "default semantics" as explained in the linked FAQ, because it may or may not default-construct members. But then we would have the same problem for in-class initializers, just that here Foo() = default;
works just fine.
So, why is it disallowed?
c++ c++11 language-lawyer default-constructor ctor-initializer
c++ c++11 language-lawyer default-constructor ctor-initializer
edited Jun 5 at 12:54
andreee
asked Jun 5 at 12:35
andreeeandreee
2,2341227
2,2341227
5
As soon as you've defined initialisers in the ctor you've provided a ctor and it's no longer the default.
– Jonathan Potter
Jun 5 at 12:38
2
Because "Default ctor with member initializer list" is not a default ctor. You can use inline initialization with "=default" though.
– Steve
Jun 5 at 12:38
1
@Quimby that's not an argument, since this is always the case. Also that's not the reason for=default
, see also the referenced C++ FAQ
– andreee
Jun 5 at 12:40
2
Could you explain what you would expect it to mean?
– molbdnilo
Jun 5 at 12:42
2
Indeed,and
=default
accomplish different things.is generally not what you want as it kills implicitly generated default move constructor/operator.
– ssteinberg
Jun 5 at 12:45
|
show 5 more comments
5
As soon as you've defined initialisers in the ctor you've provided a ctor and it's no longer the default.
– Jonathan Potter
Jun 5 at 12:38
2
Because "Default ctor with member initializer list" is not a default ctor. You can use inline initialization with "=default" though.
– Steve
Jun 5 at 12:38
1
@Quimby that's not an argument, since this is always the case. Also that's not the reason for=default
, see also the referenced C++ FAQ
– andreee
Jun 5 at 12:40
2
Could you explain what you would expect it to mean?
– molbdnilo
Jun 5 at 12:42
2
Indeed,and
=default
accomplish different things.is generally not what you want as it kills implicitly generated default move constructor/operator.
– ssteinberg
Jun 5 at 12:45
5
5
As soon as you've defined initialisers in the ctor you've provided a ctor and it's no longer the default.
– Jonathan Potter
Jun 5 at 12:38
As soon as you've defined initialisers in the ctor you've provided a ctor and it's no longer the default.
– Jonathan Potter
Jun 5 at 12:38
2
2
Because "Default ctor with member initializer list" is not a default ctor. You can use inline initialization with "=default" though.
– Steve
Jun 5 at 12:38
Because "Default ctor with member initializer list" is not a default ctor. You can use inline initialization with "=default" though.
– Steve
Jun 5 at 12:38
1
1
@Quimby that's not an argument, since this is always the case. Also that's not the reason for
=default
, see also the referenced C++ FAQ– andreee
Jun 5 at 12:40
@Quimby that's not an argument, since this is always the case. Also that's not the reason for
=default
, see also the referenced C++ FAQ– andreee
Jun 5 at 12:40
2
2
Could you explain what you would expect it to mean?
– molbdnilo
Jun 5 at 12:42
Could you explain what you would expect it to mean?
– molbdnilo
Jun 5 at 12:42
2
2
Indeed,
and =default
accomplish different things.
is generally not what you want as it kills implicitly generated default move constructor/operator.– ssteinberg
Jun 5 at 12:45
Indeed,
and =default
accomplish different things.
is generally not what you want as it kills implicitly generated default move constructor/operator.– ssteinberg
Jun 5 at 12:45
|
show 5 more comments
4 Answers
4
active
oldest
votes
= default;
is an entire definition all on its own. It's enforced, first and foremost, grammatically:
[dcl.fct.def.general]
1 Function definitions have the form
function-definition:
attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt function-body
function-body:
ctor-initializeropt compound-statement
function-try-block
= default ;
= delete ;
So it's either a member initializer list with a compound statement, or just plain = default;
, no mishmash.
Furthermore, = default
means something specific about how each member is initialized. It means we explicitly want to initialize everything like a compiler provided constructor would. That is in contradiction to "doing something special" with the members in the constructor's member initializer list.
Regarding your last paragraph: What about in-class member initializers then? definingint a = 1; int b = 2
in the class and usingFoo() = default;
works just fine. Wouldn't that be a contradiction?
– andreee
Jun 5 at 12:48
4
@andreee - No. Because if you omitFoo() = default
, the default member initializers are still in effect. That is what the compiler provided constructor will do.
– StoryTeller
Jun 5 at 12:48
Thanks, that makes sense now. I also liked @NathanOliver's answer a lot, but I like this one slightly better (and you cannot accept two questions :-)).
– andreee
Jun 5 at 13:16
add a comment |
Doing a1, b2
means you no longer can specify it as default
. A default function per [dcl.fct.def.default]/1 is defined as
A function definition whose function-body is of the form
= default;
is called an explicitly-defaulted definition.
And if we check what a function-body is in [dcl.fct.def.general]/1 we see that it contains a ctor-initializer which is a mem-initializer-list
This means you can't initialize members if you want a default definition provided by the compiler.
What you can do to work around this is to specify the default values in the class directly, and then declare the constructor as default like
class Foo
int a1, b2;
public:
Foo() = default;
;
add a comment |
This doesn't directly answer the question, however it is the c++ "way" is to use the default member initializer instead, that it
class Foo
int a = 1, b = 2;
public:
Foo() = default;
;
The syntax you purpose is simply not a default, per se, constructor anymore.
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
add a comment |
It's disallowed because what you're trying to do by definition means it's no longer a default constructor. And there's a more elegant way of accomplishing what you want anyway:
class Foo
int a 1;
int b 2;
public:
Foo() = default;
;
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
You mean, as you mentioned in an edit to your question after this answer had been posted?
– Steve
Jun 5 at 13:19
Yes. But I also mentioned in-class initializers in my original post and that it doesn't pose a problem there (second last paragraph). That was just a remark, no offense :-)
– andreee
Jun 5 at 13:20
1
a default constructor is by definition one that can be called with no arguments. see here en.cppreference.com/w/cpp/language/default_constructor
– formerlyknownas_463035818
Jun 5 at 13:37
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56460611%2fwhy-cant-i-use-default-for-default-ctors-with-a-member-initializer-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
= default;
is an entire definition all on its own. It's enforced, first and foremost, grammatically:
[dcl.fct.def.general]
1 Function definitions have the form
function-definition:
attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt function-body
function-body:
ctor-initializeropt compound-statement
function-try-block
= default ;
= delete ;
So it's either a member initializer list with a compound statement, or just plain = default;
, no mishmash.
Furthermore, = default
means something specific about how each member is initialized. It means we explicitly want to initialize everything like a compiler provided constructor would. That is in contradiction to "doing something special" with the members in the constructor's member initializer list.
Regarding your last paragraph: What about in-class member initializers then? definingint a = 1; int b = 2
in the class and usingFoo() = default;
works just fine. Wouldn't that be a contradiction?
– andreee
Jun 5 at 12:48
4
@andreee - No. Because if you omitFoo() = default
, the default member initializers are still in effect. That is what the compiler provided constructor will do.
– StoryTeller
Jun 5 at 12:48
Thanks, that makes sense now. I also liked @NathanOliver's answer a lot, but I like this one slightly better (and you cannot accept two questions :-)).
– andreee
Jun 5 at 13:16
add a comment |
= default;
is an entire definition all on its own. It's enforced, first and foremost, grammatically:
[dcl.fct.def.general]
1 Function definitions have the form
function-definition:
attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt function-body
function-body:
ctor-initializeropt compound-statement
function-try-block
= default ;
= delete ;
So it's either a member initializer list with a compound statement, or just plain = default;
, no mishmash.
Furthermore, = default
means something specific about how each member is initialized. It means we explicitly want to initialize everything like a compiler provided constructor would. That is in contradiction to "doing something special" with the members in the constructor's member initializer list.
Regarding your last paragraph: What about in-class member initializers then? definingint a = 1; int b = 2
in the class and usingFoo() = default;
works just fine. Wouldn't that be a contradiction?
– andreee
Jun 5 at 12:48
4
@andreee - No. Because if you omitFoo() = default
, the default member initializers are still in effect. That is what the compiler provided constructor will do.
– StoryTeller
Jun 5 at 12:48
Thanks, that makes sense now. I also liked @NathanOliver's answer a lot, but I like this one slightly better (and you cannot accept two questions :-)).
– andreee
Jun 5 at 13:16
add a comment |
= default;
is an entire definition all on its own. It's enforced, first and foremost, grammatically:
[dcl.fct.def.general]
1 Function definitions have the form
function-definition:
attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt function-body
function-body:
ctor-initializeropt compound-statement
function-try-block
= default ;
= delete ;
So it's either a member initializer list with a compound statement, or just plain = default;
, no mishmash.
Furthermore, = default
means something specific about how each member is initialized. It means we explicitly want to initialize everything like a compiler provided constructor would. That is in contradiction to "doing something special" with the members in the constructor's member initializer list.
= default;
is an entire definition all on its own. It's enforced, first and foremost, grammatically:
[dcl.fct.def.general]
1 Function definitions have the form
function-definition:
attribute-specifier-seqopt decl-specifier-seqopt declarator virt-specifier-seqopt function-body
function-body:
ctor-initializeropt compound-statement
function-try-block
= default ;
= delete ;
So it's either a member initializer list with a compound statement, or just plain = default;
, no mishmash.
Furthermore, = default
means something specific about how each member is initialized. It means we explicitly want to initialize everything like a compiler provided constructor would. That is in contradiction to "doing something special" with the members in the constructor's member initializer list.
edited Jun 5 at 12:50
answered Jun 5 at 12:43
StoryTellerStoryTeller
112k18239303
112k18239303
Regarding your last paragraph: What about in-class member initializers then? definingint a = 1; int b = 2
in the class and usingFoo() = default;
works just fine. Wouldn't that be a contradiction?
– andreee
Jun 5 at 12:48
4
@andreee - No. Because if you omitFoo() = default
, the default member initializers are still in effect. That is what the compiler provided constructor will do.
– StoryTeller
Jun 5 at 12:48
Thanks, that makes sense now. I also liked @NathanOliver's answer a lot, but I like this one slightly better (and you cannot accept two questions :-)).
– andreee
Jun 5 at 13:16
add a comment |
Regarding your last paragraph: What about in-class member initializers then? definingint a = 1; int b = 2
in the class and usingFoo() = default;
works just fine. Wouldn't that be a contradiction?
– andreee
Jun 5 at 12:48
4
@andreee - No. Because if you omitFoo() = default
, the default member initializers are still in effect. That is what the compiler provided constructor will do.
– StoryTeller
Jun 5 at 12:48
Thanks, that makes sense now. I also liked @NathanOliver's answer a lot, but I like this one slightly better (and you cannot accept two questions :-)).
– andreee
Jun 5 at 13:16
Regarding your last paragraph: What about in-class member initializers then? defining
int a = 1; int b = 2
in the class and using Foo() = default;
works just fine. Wouldn't that be a contradiction?– andreee
Jun 5 at 12:48
Regarding your last paragraph: What about in-class member initializers then? defining
int a = 1; int b = 2
in the class and using Foo() = default;
works just fine. Wouldn't that be a contradiction?– andreee
Jun 5 at 12:48
4
4
@andreee - No. Because if you omit
Foo() = default
, the default member initializers are still in effect. That is what the compiler provided constructor will do.– StoryTeller
Jun 5 at 12:48
@andreee - No. Because if you omit
Foo() = default
, the default member initializers are still in effect. That is what the compiler provided constructor will do.– StoryTeller
Jun 5 at 12:48
Thanks, that makes sense now. I also liked @NathanOliver's answer a lot, but I like this one slightly better (and you cannot accept two questions :-)).
– andreee
Jun 5 at 13:16
Thanks, that makes sense now. I also liked @NathanOliver's answer a lot, but I like this one slightly better (and you cannot accept two questions :-)).
– andreee
Jun 5 at 13:16
add a comment |
Doing a1, b2
means you no longer can specify it as default
. A default function per [dcl.fct.def.default]/1 is defined as
A function definition whose function-body is of the form
= default;
is called an explicitly-defaulted definition.
And if we check what a function-body is in [dcl.fct.def.general]/1 we see that it contains a ctor-initializer which is a mem-initializer-list
This means you can't initialize members if you want a default definition provided by the compiler.
What you can do to work around this is to specify the default values in the class directly, and then declare the constructor as default like
class Foo
int a1, b2;
public:
Foo() = default;
;
add a comment |
Doing a1, b2
means you no longer can specify it as default
. A default function per [dcl.fct.def.default]/1 is defined as
A function definition whose function-body is of the form
= default;
is called an explicitly-defaulted definition.
And if we check what a function-body is in [dcl.fct.def.general]/1 we see that it contains a ctor-initializer which is a mem-initializer-list
This means you can't initialize members if you want a default definition provided by the compiler.
What you can do to work around this is to specify the default values in the class directly, and then declare the constructor as default like
class Foo
int a1, b2;
public:
Foo() = default;
;
add a comment |
Doing a1, b2
means you no longer can specify it as default
. A default function per [dcl.fct.def.default]/1 is defined as
A function definition whose function-body is of the form
= default;
is called an explicitly-defaulted definition.
And if we check what a function-body is in [dcl.fct.def.general]/1 we see that it contains a ctor-initializer which is a mem-initializer-list
This means you can't initialize members if you want a default definition provided by the compiler.
What you can do to work around this is to specify the default values in the class directly, and then declare the constructor as default like
class Foo
int a1, b2;
public:
Foo() = default;
;
Doing a1, b2
means you no longer can specify it as default
. A default function per [dcl.fct.def.default]/1 is defined as
A function definition whose function-body is of the form
= default;
is called an explicitly-defaulted definition.
And if we check what a function-body is in [dcl.fct.def.general]/1 we see that it contains a ctor-initializer which is a mem-initializer-list
This means you can't initialize members if you want a default definition provided by the compiler.
What you can do to work around this is to specify the default values in the class directly, and then declare the constructor as default like
class Foo
int a1, b2;
public:
Foo() = default;
;
edited Jun 5 at 18:39
René Richter
2,56012235
2,56012235
answered Jun 5 at 12:51
NathanOliverNathanOliver
105k19155231
105k19155231
add a comment |
add a comment |
This doesn't directly answer the question, however it is the c++ "way" is to use the default member initializer instead, that it
class Foo
int a = 1, b = 2;
public:
Foo() = default;
;
The syntax you purpose is simply not a default, per se, constructor anymore.
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
add a comment |
This doesn't directly answer the question, however it is the c++ "way" is to use the default member initializer instead, that it
class Foo
int a = 1, b = 2;
public:
Foo() = default;
;
The syntax you purpose is simply not a default, per se, constructor anymore.
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
add a comment |
This doesn't directly answer the question, however it is the c++ "way" is to use the default member initializer instead, that it
class Foo
int a = 1, b = 2;
public:
Foo() = default;
;
The syntax you purpose is simply not a default, per se, constructor anymore.
This doesn't directly answer the question, however it is the c++ "way" is to use the default member initializer instead, that it
class Foo
int a = 1, b = 2;
public:
Foo() = default;
;
The syntax you purpose is simply not a default, per se, constructor anymore.
edited Jun 5 at 13:09
answered Jun 5 at 12:44
ssteinbergssteinberg
5,51033059
5,51033059
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
add a comment |
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
add a comment |
It's disallowed because what you're trying to do by definition means it's no longer a default constructor. And there's a more elegant way of accomplishing what you want anyway:
class Foo
int a 1;
int b 2;
public:
Foo() = default;
;
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
You mean, as you mentioned in an edit to your question after this answer had been posted?
– Steve
Jun 5 at 13:19
Yes. But I also mentioned in-class initializers in my original post and that it doesn't pose a problem there (second last paragraph). That was just a remark, no offense :-)
– andreee
Jun 5 at 13:20
1
a default constructor is by definition one that can be called with no arguments. see here en.cppreference.com/w/cpp/language/default_constructor
– formerlyknownas_463035818
Jun 5 at 13:37
add a comment |
It's disallowed because what you're trying to do by definition means it's no longer a default constructor. And there's a more elegant way of accomplishing what you want anyway:
class Foo
int a 1;
int b 2;
public:
Foo() = default;
;
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
You mean, as you mentioned in an edit to your question after this answer had been posted?
– Steve
Jun 5 at 13:19
Yes. But I also mentioned in-class initializers in my original post and that it doesn't pose a problem there (second last paragraph). That was just a remark, no offense :-)
– andreee
Jun 5 at 13:20
1
a default constructor is by definition one that can be called with no arguments. see here en.cppreference.com/w/cpp/language/default_constructor
– formerlyknownas_463035818
Jun 5 at 13:37
add a comment |
It's disallowed because what you're trying to do by definition means it's no longer a default constructor. And there's a more elegant way of accomplishing what you want anyway:
class Foo
int a 1;
int b 2;
public:
Foo() = default;
;
It's disallowed because what you're trying to do by definition means it's no longer a default constructor. And there's a more elegant way of accomplishing what you want anyway:
class Foo
int a 1;
int b 2;
public:
Foo() = default;
;
answered Jun 5 at 12:43
SteveSteve
1,752618
1,752618
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
You mean, as you mentioned in an edit to your question after this answer had been posted?
– Steve
Jun 5 at 13:19
Yes. But I also mentioned in-class initializers in my original post and that it doesn't pose a problem there (second last paragraph). That was just a remark, no offense :-)
– andreee
Jun 5 at 13:20
1
a default constructor is by definition one that can be called with no arguments. see here en.cppreference.com/w/cpp/language/default_constructor
– formerlyknownas_463035818
Jun 5 at 13:37
add a comment |
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
You mean, as you mentioned in an edit to your question after this answer had been posted?
– Steve
Jun 5 at 13:19
Yes. But I also mentioned in-class initializers in my original post and that it doesn't pose a problem there (second last paragraph). That was just a remark, no offense :-)
– andreee
Jun 5 at 13:20
1
a default constructor is by definition one that can be called with no arguments. see here en.cppreference.com/w/cpp/language/default_constructor
– formerlyknownas_463035818
Jun 5 at 13:37
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
Thanks, but as mentioned in my question, this is not what I was interested in.
– andreee
Jun 5 at 13:18
You mean, as you mentioned in an edit to your question after this answer had been posted?
– Steve
Jun 5 at 13:19
You mean, as you mentioned in an edit to your question after this answer had been posted?
– Steve
Jun 5 at 13:19
Yes. But I also mentioned in-class initializers in my original post and that it doesn't pose a problem there (second last paragraph). That was just a remark, no offense :-)
– andreee
Jun 5 at 13:20
Yes. But I also mentioned in-class initializers in my original post and that it doesn't pose a problem there (second last paragraph). That was just a remark, no offense :-)
– andreee
Jun 5 at 13:20
1
1
a default constructor is by definition one that can be called with no arguments. see here en.cppreference.com/w/cpp/language/default_constructor
– formerlyknownas_463035818
Jun 5 at 13:37
a default constructor is by definition one that can be called with no arguments. see here en.cppreference.com/w/cpp/language/default_constructor
– formerlyknownas_463035818
Jun 5 at 13:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56460611%2fwhy-cant-i-use-default-for-default-ctors-with-a-member-initializer-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
As soon as you've defined initialisers in the ctor you've provided a ctor and it's no longer the default.
– Jonathan Potter
Jun 5 at 12:38
2
Because "Default ctor with member initializer list" is not a default ctor. You can use inline initialization with "=default" though.
– Steve
Jun 5 at 12:38
1
@Quimby that's not an argument, since this is always the case. Also that's not the reason for
=default
, see also the referenced C++ FAQ– andreee
Jun 5 at 12:40
2
Could you explain what you would expect it to mean?
– molbdnilo
Jun 5 at 12:42
2
Indeed,
and
=default
accomplish different things.is generally not what you want as it kills implicitly generated default move constructor/operator.
– ssteinberg
Jun 5 at 12:45