c++c++17language-lawyertemporary-objects

Does std::string {} = "hi"; induce temporary materialization?


Motivated by What does the expression std::string {} = "..." mean?;

Does the left hand side of the std::string {} = "hi"; induce temporary materialization and if it does, which of the mentioned scenarios below does it fall within?

Temporary materialization occurs in the following situations:

  • 1- when binding a reference to a prvalue;
  • 2- when performing a member access on a class prvalue;
  • 3- when performing an array-to-pointer conversion or subscripting on an array prvalue;
  • 4- when initializing an object of type std::initializer_list from a braced-init-list;
  • 5- when typeid is applied to a prvalue
  • 6- when sizeof is applied to a prvalue
  • 7- when a prvalue appears as a discarded-value expression.

I would expect that std::string {} induces temporary materialization since we create an object that is temporary, but couldn't find which scenario would fit here.


Solution

  • The expression is transformed (after overload resolution) to std::string{}.operator=("hi") by [over.match.oper]/2, at which point temporary materialization applies per the second point in the list. (In standardese, [expr.ref]/2 says that . expects its first operand to be a glvalue, and [basic.lval]/7 applies the temporary materialization conversion whenever a prvalue appears in such a context.)