Why does this program output doubledouble , I explicitly specify an int ?!

 struct A { explicit A(int) { std::cout << "int"; }; A(double) { std::cout << "double"; }; }; int main() { A a1 = 42; A a2 = int(42); } 
  • There is no difference at all between a1 and a2. And here is a little explicit - αλεχολυτ

1 answer 1

Using the copy initialization syntax you just explicitly tell not to use the explicit constructor. To call the explicit constructor, use the direct list initialization syntax:

 a1{42}; A a2{int{42}}; // выводится intint