Krystian S
Krystian S

Reputation: 1616

Temporary materialization conversion not in a standard conversion sequence

Temporary materialization conversion is a standard conversion, see § 7.3 Standard Conversions; 7.3.4 [conv.rval]:

A prvalue of type T can be converted to an xvalue of type T. This conversion initializes a temporary object ([class.temporary]) of type T from the prvalue by evaluating the prvalue with the temporary object as its result object, and produces an xvalue denoting the temporary object. T shall be a complete type.

But why is it not mentioned in the list of standard conversion sequences?

See [conv]/1:

A standard conversion sequence is a sequence of standard conversions in the following order:

  • Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.

  • Zero or one conversion from the following set: integral promotions, floating-point promotion, integral conversions, floating-point conversions, floating-integral conversions, pointer conversions, pointer-to-member conversions, and boolean conversions.

  • Zero or one function pointer conversion.

  • Zero or one qualification conversion.

Is it because an object would have to be created either way, and therefore would have no impact on determining whether a conversion sequence is better than another?

Upvotes: 1

Views: 488

Answers (1)

rustyx
rustyx

Reputation: 85452

Is it because an object would have to be created either way, and therefore would have no impact on determining whether a conversion sequence is better than another?

Yes, temporary materialization is not a choice, so it's "free" and exempt from ICS ranking. It's applied when needed, see [expr.basic.lval]/7:

Whenever a prvalue appears as an operand of an operator that expects a glvalue for that operand, the temporary materialization conversion is applied to convert the expression to an xvalue.

For example, the dot-expression requires the left-hand side to be a glvalue:

struct X {
  void func(int) { }
  void func(long) { }
};

int n = 1;

X().func(n);

Here the X() prvalue must first become an xvalue (materialized) before we can proceed to .func(n), at which time ICS ranking comes into picture to decide how to invoke func(n), since there can be different conversion sequences leading to different alternatives.

Upvotes: 2

Related Questions