This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 114a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-04-28


2434. Mandatory copy elision vs non-class objects

Section: 6.7.7  [class.temporary]     Status: review     Submitter: Richard Smith     Date: 2019-09-30

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.

Possible resolution:

Change in 6.7.7 [class.temporary] paragraph 3 as follows:

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). Similarly, implementations are permitted to create a temporary object of scalar type to hold a function parameter or result object. [Note 4: This latitude is granted to allow objects of scalar or class type to be passed to or returned from functions in registers. —end note]