Title
Mandatory copy elision vs non-class objects
Status
review
Section
6.7.7 [class.temporary]
Submitter
Richard Smith

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

Messages

Date: 2024-01-22.23:18:53

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]
History
Date User Action Args
2024-01-22 23:18:53adminsetstatus: open -> review
2019-09-30 00:00:00admincreate