0

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.

domdrag
  • 541
  • 1
  • 4
  • 13
  • Also see [this answer: `std::string{} = "hello"s;` compiles](https://stackoverflow.com/a/65462050/12002570) – Jason Feb 26 '23 at 13:59
  • Do people who close questions even bother reading the question before closing it? Smh – domdrag Feb 26 '23 at 13:59
  • A) there is no temporary materialization as can already be seen from your listed bullet points B) Reason it is allowed is because class rvalue can be assigned to. – Jason Feb 26 '23 at 14:01
  • @JasonLiam You haven't read my question... – domdrag Feb 26 '23 at 14:02
  • 1
    I certainly did. You're asking whether temporary materialization is involved here. The answer to which is "no". Now, if you read the dupes carefully you'll understand why there is no temporary materialization because by reading those answers you'll understand what is happening under the hood. Read the dupes carefully before writing comments. Hint: class rvalues can be assigned to. – Jason Feb 26 '23 at 14:03
  • @JasonLiam How on Earth would this question be considered as a duplicate to any of the questions you linked? None of those posts discuss about whether temporary materialization is involved or not. Some of the answers mention the word "temporary" without any justification. Your comment "No, there is no temporary materialization here" came without any clarifications. The intent in my questions is pretty clear; at the end I said I expect it to be a temp since it's lifetime falls in one full-expression. You and none of the posts you linked don't elaborate on that. – domdrag Feb 26 '23 at 14:11
  • Because once you understood what is happening under the hood(which you will using the dupes), then you should be able to figure out the reason there is no temporary materialization involved. Not everything should be spoonfeeded. – Jason Feb 26 '23 at 14:12
  • @JasonLiam Yeah, just as if I read 1000 C++ books as well. But that is no reason to flag a question as a duplicate, is it? – domdrag Feb 26 '23 at 14:13

1 Answers1

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

T.C.
  • 133,968
  • 17
  • 288
  • 421