Can anyone explain why the following code compiles? I expect it to get an error where the double constant 3.3 can not be converted to int, since I declare the constructor to be explicit.
class A
{
public:
int n;
explicit A(int _n);
};
A::A(int _n)
{
n = _n;
}
int main()
{
A a(3.3); // <== I expect this line to get an error.
return 0;
}
A b = 24;A a{3.3};would fail because it's a narrowing conversion (double to int). HoweverA a(3.3);does not because narrowing conversions are allowed.