according to value initialization described in this page https://en.cppreference.com/w/cpp/language/value_initialization
If T is a class type that has no default constructor but has a constructor taking std::initializer_list, list-initialization is performed.
so I was expecting when initialize the class in bellow code snippet will invoke Myclass(const std::initializer_list<int> &l)
, but the compiler says
> the default constructor of "Myclass" cannot be referenced -- it is a deleted function
why is that? this is the code, I compiled with Mingw64 C++11 on windows.
#include <iostream>
class Myclass {
public:
Myclass() = delete;
Myclass(Myclass &&m) {}
Myclass(const Myclass &m) {}
Myclass(const std::initializer_list<int> &l) { std::cout << "initializer list"; }
};
int main(int argc, char const *argv[]) {
Myclass m2 {};
Myclass m1={};
}
Myclass
does have a default constructor; which is just marked asdelete
explicitly. So the effect of value-initialization should be:In default-initialization the deleted default constructor is selected and the program is ill-formed.
If not to declare the default constructor as
Then
Myclass
doesn't have default constructor; (and no implicitly-declared default constructor because of other user-declared constructors). Then list-initialization is performed (as you expected), as the effect