A default member initialisation needs to reference an existing constructor, no matter if it is ever used or not. So, looking at a struct Foo
which has no default constructor:
struct Foo{
Foo(int x) : x_(x){}
int x_;
};
It is clear that the following wouldn't work, and leads to a compilation error:
class Bar0{
Foo foo = Foo(); #constructor Foo() doesn't exist
Bar0() : foo(0){}
}
But, it is a different story with std::unique_ptr
and std::make_unique
:
class Bar1{
unique_ptr<Foo> foo = make_unique<Foo>(); #compiler doesn't complain
Bar1() : foo(make_unique<Foo>(0)){}
}
This is puzzling, as the compilation fails as soon as Bar1
contains one constructor where foo
is not in the initialiser list.
I can confirm this to be true of MSVC12. Could it be a compiler bug?