Mandatory copy elision vs non-class objects
6.7.7 [class.temporary]
Richard Smith

Created on 2019-09-30.00:00:00 last changed 18 months ago


Date: 2019-09-30.00:00:00

In the following example,

  int f() {
    X x;
    return 4;
  int a = f();

a must be directly initialized in the return statement of f() because the exception permitting temporaries for function arguments and return types in 6.7.7 [class.temporary] paragraph 3 applies only to certain class types:

When an object of class type X is passed to or returned from a function, if X has at least one eligible copy or move constructor (11.4.4 [special]), each such constructor is trivial, and the destructor of X is either trivial or deleted, implementations are permitted to create a temporary object to hold the function parameter or result object. The temporary object is constructed from the function argument or return value, respectively, and the function's parameter or return object is initialized as if by using the eligible trivial constructor to copy the temporary (even if that constructor is inaccessible or would not be selected by overload resolution to perform a copy or move of the object). [Note: This latitude is granted to allow objects of class type to be passed to or returned from functions in registers. —end note]

This requirement is observable, since the destructor of X in the example could inspect the value of a.

The permissions in this paragraph should also apply to all non-class types.

Date User Action Args
2019-09-30 00:00:00admincreate