Why can't I use =default for default ctors with a 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.
Doing a{1}, b{2}
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 a{1}, b{2};
public:
Foo() = default;
};
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.