weird class ctor

#include <bits/stdc++.h>
using namespace std;

class C {
public:
int ft, sd;
C(int foo,int bar): ft(foo), sd(bar) {
cout << "ctor 1\n";
}
C(const C &other): ft(other.ft), sd(other.sd) {
cout << "ctor 2\n";
}
C operator +(const C &right) {
cout << "ctor 3\nflag1\n";
C tmp(ft,sd);
tmp.ft += right.ft;
tmp.sd += right.sd;
return tmp;
}
};

int main() {
C first(1,2);
C second(3,4);
C third(first + second);
cout << "flag2\n";
C forth(third);
cout << "\nend\n";
return 0;
}

Output:
ctor 1
ctor 1
ctor 3
flag1
ctor 1
flag2
ctor 2

end

The question is: shouldn't the fifth line be ctor 2??
Last edited on
fifth line is output from line C tmp(ft,sd);. Actual creation of third object does not output anything as compiler probably performs RVO and just creates tmp in place of third value.

EDIT: standard wording:

2011 Standard wrote:
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 copy/move constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different 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.123 This elision of copy/move
operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):
in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cvunqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
— in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object
— 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
— when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as the exception object (15.1), the copy/move operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.
Last edited on
Thanks for your reply! @MiiNiPaa
So is it not a good idea to have printing statements in a constructor?
It is not a good idea for constructor to have side effects which can make your program to behave differently if copy elision is performed. (outputting to screen is a side effect).

For example implementing reference counting is OK, but relying on copy operations performing some task which should be in synch with other task is not (as copy elision can be performed, but not required).
Topic archived. No new replies allowed.