std::string {} = “hi”;引发临时材料化吗?

huangapple go评论72阅读模式
英文:

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

问题

C++表达式std::string {} = "..."的启发;

std::string {} = "hi"; 的左侧是否引发了临时材料化,如果是的话,它将属于以下提到的情况之一?

临时材料化发生在以下情况下:

  • 1- 当将引用绑定到prvalue时;
  • 2- 当在类prvalue上执行成员访问时;
  • 3- 当在数组prvalue上执行数组到指针转换或对数组prvalue进行下标引用时;
  • 4- 当从大括号初始化列表初始化std::initializer_list类型的对象时;
  • 5- 当typeid应用于prvalue时;
  • 6- 当sizeof应用于prvalue时;
  • 7- 当prvalue出现为弃值表达式时。

我预期std::string {}引发了临时材料化,因为我们创建了一个临时对象,但我找不到适用的情况。

英文:

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<T> 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.

答案1

得分: 3

The expression is transformed (after overload resolution) to std::string{}.operator=(&quot;hi&quot;) 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.)

英文:

The expression is transformed (after overload resolution) to std::string{}.operator=(&quot;hi&quot;) 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.)

huangapple
  • 本文由 发表于 2023年2月26日 21:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定