1

I've read that those two syntaxes aren't the same when instantiating an object:

ClassName c1(p);

and

ClassName c1 = ClassName(p);

(or without parameters due to constructor)

I've read the second syntax creates two objects one to be temporary with given parameters then copy constructor gets called. But when I create an object the second way, the cout in the constructor and destructor prints one time each. That leads me to think if I am misinformed and these two ways are completely the same or the compiler automatically optimizes it and that causes it?

If it's because optimization is there a way to disable the optimization to see the difference between them?

alexmoran
  • 21
  • 1
  • The two lines are exactly equivalent since C++17 (there is no temporary) and even before that the temporary copy was allowed to be elided (as you observe). I am sure there is a good duplicate somewhere. – walnut Apr 07 '20 at 14:03
  • The two syntaxes aren't the same. One is direct-initialization, the other is copy-initialization. They may (and usually do) result in the same code at runtime, however. – Ben Voigt Apr 07 '20 at 14:55

0 Answers0