copy elision: move constructor not called when using ternary expression in return statement?

The specification for the conditional operator is so complicated it is scary. But I believe that your compiler is correct in its behavior. See 5.16 [expr.cond]/p4:

If the second and third operands are glvalues of the same value
category and have the same type, the result is of that type and value
category …

Also see 12.8 [class.copy], p31, b1 which describes when copy elision is allowed:

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 cv-
unqualified type as the function return type, the copy/move operation
can be omitted …

The expression is not the name of an automatic object, but is a conditional expression (and that conditional expression is an lvalue). So copy elision is not allowed here, and there is nothing else that says that the lvalue expression can pretend to be an rvalue for overload resolution.

Leave a Comment