Firstly, I am right in assuming that the zero initialization of the member variable n is dependent on the implementation of the compiler and the standard is NOT guaranteed (or has it changed using C ++ 11)?
No, S() means initializing the value in both C ++ 03 and C ++ 11. Although I believe that the wording of this is much clearer in C ++ 11 than C ++ 03. In this case, the initialization of the value goes over to zero initialization. Contrast this with default initialization, which is non-zero:
S s1; // default initialization std::cout << s1.n << '\n'; // prints garbage, crank up optimizer to show S s2 = S(); // value initialization std::cout << s2.n << '\n'; // prints 0
Secondly, why is f (X) called with C ++ 03, and not C ++ 11? I would suggest that f (void *) will hit regardless of the value of S (). n over the implicit conversion to X
In C ++ 03, an int never become a null pointer constant. Once 0 is typed as an int , say by assigning it to int , then it is forever an int , and not a constant of a null pointer.
In C ++ 11, S().n implicitly a constexpr expression with a value of 0 , and a constexpr expression with a value of 0 can be a null pointer constant.
Finally, this is not an intentional change on the part of the committee, as far as I can tell. If you write code that depends on this difference, you can bite in the future if / when the committee recovers. I would do well in this area. Use it to win bets in a bar - not in the production code.
source share