Consider the following class:
class Foo {
int a, b;
public:
Foo() : a{1}, b{2} {} // Default ctor with member initializer list
//Foo() : a{1}, b{2} = default; // Does not work but why?
};
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?
Aucun commentaire:
Enregistrer un commentaire