1

In the C++ standard one can find examples of prvalue expressions:

"prvalue

The following expressions are prvalue expressions:

a literal (except for string literal), such as 42, true or nullptr;

a function call or an overloaded operator expression of non-reference return type, such as str.substr(1, 2), str1 + str2, or it++; ..."

Is this where an expression such as T() (a constructor call) would go?

Is there another name for such expressions?

user42768
  • 1,951
  • 11
  • 22

1 Answers1

4

Although the particular case of a default constructor puzzles me a bit, this is considered a cast expression, which is a bit lower on the list:

  • a cast expression to non-reference type, such as static_cast<double>(x), std::string{}, or (int)42;

Even though the idea of converting nothing is peculiar, the T() syntax is indeed covered as form #4 here.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • Is it considered a cast because the initializer (the parameters of the constructor are called 'initializer', am I right?) is converted to type `T`? – user42768 May 23 '17 at 18:31
  • 1
    I found another [page](http://en.cppreference.com/w/cpp/language/direct_initialization) where this specific expression is mentioned: "3) initialization of a prvalue temporary by functional cast or with a parenthesized expression list". It seems strange (for me) to refer to a constructor call as a cast, but if that is how the standard refers to it so be it. – user42768 May 23 '17 at 18:54
  • @user42768 that's the gist of it, yes. – Quentin May 23 '17 at 19:11
  • Ok. So a `direct initialization` (as in the page that I linked in my previous comment) is still considered to be a cast. Thank you! – user42768 May 23 '17 at 19:17
  • @user42768 only if it's creating a temporary, if memory serves. – Quentin May 23 '17 at 19:22
  • Yes, sorry, that is what I meant. Does the standard explicitly mention this? – user42768 May 23 '17 at 19:28
  • @user42768 it's about time I think of mentioning this: "cast" and "explicit conversion" are interchangeable. It's about constructing a temporary `T` from some (or no) original object(s). – Quentin May 23 '17 at 19:33
  • I found in the [link](http://en.cppreference.com/w/cpp/language/explicit_cast) you provided at point 3: "new_type ( expressions ) (3)". This is what I was looking for. Thank you again! – user42768 May 23 '17 at 19:38