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); } 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); } 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 Source: https://ru.stackoverflow.com/questions/907081/
All Articles
explicit- αλεχολυτ