jeudi 15 octobre 2020

why is list initialization not invoked when initialize this class?

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={};
}

Aucun commentaire:

Enregistrer un commentaire