英文:
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=("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.)
英文:
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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论