Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why decltype(a, b) is evaluated to a reference?

According to this answer, ref should be an int.

But for some reason it evaluated to int&, both in gcc and MSVC2015, while decltype(b) is correctly evaluated to just int. Why so?

int a = 1, b = 2;
decltype(a, b) ref; // ref is int&
decltype(b) var;    // var is int
like image 768
Starl1ght Avatar asked Jun 26 '16 17:06

Starl1ght


People also ask

Is a decltype an lvalue reference?

Otherwise, E ’s exprtype is an lvalue reference. An unintuitive consequence of this rule is that the reference qualification on E ’s expression decltype is generally independent of that on its variable decltype. For example, v and vref have the same exprtype, as do s.lref and s.rref.

How to make it reference type in C++?

To make it reference type, we use auto &. 2) decltype Keyword: It inspects the declared type of an entity or the type of an expression. ‘auto’ lets you declare a variable with a particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.

When to use decltype in C++?

If there is no such entity, or if the argument names a set of overloaded functions, the program is ill-formed. If the argument is an unparenthesized id-expression naming a structured binding, then decltype yields the referenced type (described in the specification of the structured binding declaration).

What is the difference between auto declare type and decltype?

decltype Keyword It inspects the declared type of an entity or the type of an expression. Auto lets you declare a variable with particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression. Explanation of above keyword and their uses is given below:


1 Answers

a, b is an expression. According to decltype rules for expressions, if result of the expression is an lvalue, type is going to be deduced as T&

7.1.6.2/4 Simple type specifiers [dcl.type.simple]
For an expression e, the type denoted by decltype(e) is defined as follows:

  • if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;
  • otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
  • otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
  • otherwise, decltype(e) is the type of e.

The confusing part about difference between "type of the entity named by e" and "type of e" is easy to understand with example:

If some entity e is declared as int& e = x;, then later, in expression e, type of e is int, and type of the entity named by e is int&. In short, type of e drops reference qualifiers.

like image 136
Revolver_Ocelot Avatar answered Sep 20 '22 19:09

Revolver_Ocelot