struct A
{
A(int) {}
A(std::initializer_list<int>) {}
};
template<typename T>
struct B
{
B(A) {}
B(std::initializer_list<T>) {}
};
int main()
{
int n{};
B m1(A{n}); // error: call A::A(std::initializer_list<int>)
B m2(A(n)); // error: just a function declaration: B m2(A n);
}
As shown in the code above, I want to call B::B(A(int)) to construct an object of class B. I have two choices:
B m1(A{n});B m1(A(n));
According to C++ Core Guidelines, the former is preferred.
However, B m1(A{n}); will call A::A(std::initializer_list<int>) rather than A::A(int), which is not intended. So, I have to use B m1(A(n));, but it is just a function declaration: B m2(A n);!
How to solve ambiguous declarations elegantly in C++11?
Aucun commentaire:
Enregistrer un commentaire