mardi 6 décembre 2016

why in this case copy ctor is matched?

I am curious about the C++11 list-initialization.I defined a class.

class base{
base() { cout << "default ctor" << endl;}
base(std::initiazer_list<base> il) { cout << "list initialization << endl;}
base(const base &rhs) { cout << "copy ctor" << endl;}
}

In main function, I initialize two object to test my class.

int main()
{
base obj{}; // default ctor
base obj2{obj}; // copy ctor
}

I learn from 《effective modern C++》 that calls using list-initialization syntax strongly prefer the overloads that taking std::initializer_list. So, in my case, I think the second ctor will be called, however, the third is called. Can you tell my reason? I really appreciate your help!

Aucun commentaire:

Enregistrer un commentaire