My best guess is compiler optimization. There is a valid way to declare an object this way if you have a copy constructor. Think about it explicitly:
int main() { X x(X(1)); return 0; }
Or more explicitly:
int main() { X intermediate(1); X x(intermediate); return 0; }
So instead of using operator= it knows that you are trying to initialize your object from the moment it is declared. In essence, the compiler is smart. Finally, he optimizes this step again:
int main() { X x(1); return 0; }
Therefore, after the compiler finds out “what you tried to do”, it becomes the standard initialization of the object.
EDIT
For completeness, note that if you try this:
int main() { X x = 1; x = 2; return 0; }
You will see a problem with private operator= . Obviously, it’s important to note that operator= never used in the original initialization question above, although = appears in the code.
Raaged
source share