Currently trying to wrap my head around C++11's uniform initialization. I came upon this ambiguous case: consider a class which can either be constructed from either a two-argument constructor or an initializer list of any length:
class Foo {
public:
Foo(int a, int b) {
std::cout << "constructor 1" << std::endl;
}
Foo(std::initializer_list<int>) {
std::cout << "constructor 2" << std::endl;
}
};
Following uniform initialization convention, I'd expect the following to work:
Foo a (1, 2)
prints constructor 1
(duh)
Foo b {1, 2}
prints constructor 1
Foo c = {1, 2}
prints constructor 2
However, it seems like the compiler interprets Foo b {1, 2}
as a list initialization, and calls constructor 2. Is the ()
syntax the only way to force the compiler to consider other kinds of constructors when an initializer-list constructor is present?
Aucun commentaire:
Enregistrer un commentaire