Home  >  Article  >  Backend Development  >  Why Does `decltype((...))` Return a Reference While `decltype(...)` Doesn\'t?

Why Does `decltype((...))` Return a Reference While `decltype(...)` Doesn\'t?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-02 04:31:30976browse

 Why Does `decltype((...))` Return a Reference While `decltype(...)` Doesn't?

Understanding the Significance of Parentheses in decltype((...))

The use of double parentheses in decltype((...)) has a subtle but important impact on the type deduced by the decltype operator, as evidenced in the example from the C 17 Function Definition Concurrency (FCD).

When applied to an unparenthesized identifier (id-expression) or a class member access expression, decltype returns the type of the referenced entity. However, when the expression is an lvalue (a variable or expression referring to a memory location), decltype prefixes an additional reference (&) before the entity's type.

In the FCD example:

  • decltype(a->x) references a member variable (double x) of a class instance (*a) and thus returns the base type of the member: double.
  • decltype((a->x)) wraps the same member access expression within parentheses, treating it as an lvalue. As a result, decltype returns the type of the lvalue, which is a reference to the double type: const double&.

The above is the detailed content of Why Does `decltype((...))` Return a Reference While `decltype(...)` Doesn\'t?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn