Let’s continue with constructors. Before C++11, each class would have implicit constructors unless stated otherwise. One example of a situation where an implicit constructor would not have been created was the explicit declaration of a custom constructor by the programmer. For example:
struct foo { foo(int) {} }; int main() { foo f1; // fail foo f2(24); // ok foo f3(f2); // ok return 0; }
In the above example, foo has no default constructor (but has an implicit copy constructor). In order to have the default constructor, the programmer had to implement one. The main problem becomes maintenance: when new fields are added in the class, we have to remember to update the constructor to initialize them.
Starting with C++11, the programmer can tell the compiler to implement the constructor with what would have been the default implementation if it was not deleted.
struct foo
{
foo()=default;
foo(int) {}
};
int main()
{
foo f1; //
ok
foo f2(24); // ok
foo f3(f2); // ok
return 0;
}