2

Seen this in a book I'm reading:

Rectangle r(Point(200,200));

Is this the same as:

Rectangle r = Rectangle(Point(200,200));
BugHunterUK
  • 8,346
  • 16
  • 65
  • 121

1 Answers1

2

In:

Rectangle r(Point(200,200));

you are initializing a Rectangle object via a constructor that takes a Point object.

In:

Rectangle r = Rectangle(Point(200,200));

you are constructing a Rectangle temporary object as above and then passing it to the copy/move constructor of Rectangle.

If the copy constructor is properly written, then the resulting objects are the same, but one more copy/move constructor would be theoretically called in the latter.

This is not true if the compiler decides to elide the copy, according to §12.8/31:

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side eects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two dierent ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

[...]

  • when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

[...]

If the contructor that takes a Point is not marked explicit, then you could also have the form:

Rectangle r = Point(200,200);
Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 1
    "you are constructing a Rectangle temporary object as above and then passing it to the copy/move constructor" Not when [return value optimization](https://en.wikipedia.org/wiki/Return_value_optimization) is enabled ([demo](http://ideone.com/yy3BWZ)). – Sergey Kalinichenko Jun 25 '15 at 16:52
  • 1
    @dasblinkenlight I don't think it's RVO, but copy elision. Your point stands, though. – Quentin Jun 25 '15 at 16:53
  • There are no extra calls in this example: http://coliru.stacked-crooked.com/a/65ddac91a997013d – NathanOliver Jun 25 '15 at 17:00
  • 1
    @dasblinkenlight Yes, it's copy elision. Thanks. – Shoe Jun 25 '15 at 17:02
  • @Quentin Right, added. – Shoe Jun 25 '15 at 17:02
  • @NathanOliver Never claimed there would be in that case, sir. – Shoe Jun 25 '15 at 17:03
  • @Jefffrey you did with your original answer. `but one more copy/move constructor is being called in the latter.` – NathanOliver Jun 25 '15 at 17:18
  • @NathanOliver In my original answer I was talking about `Rectangle r = Rectangle(Point(200,200));`, not `Rectangle r = Point(200,200);`. And that is exactly what the standard says happens. It also says that compilers are allowed to omit that copy/move, which is what was pointed out by dasblinkenlight and Quentin above. – Shoe Jun 25 '15 at 19:18