Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++11 decltype(e) is the type of the entity named by e


I am not asking decltype((x)), I know how it works.

According to the draft N4687, § 10.1.7.2

    4 For an expression e, the type denoted by decltype(e) is defined as follows:
        ...
(4.2)   — otherwise, if e is an unparenthesized id-expression or an unparenthesized class
          member access (8.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;
        ...

And example

struct A { double x; };
const A* a = new A();
decltype(a->x) x3; // type is double

My question is,
a->x is const double, but why does x3 is double? where does the const go?
BTW, what is decltype(e) is the type of the entity named by e meaning exactly?

like image 836
Caesar Avatar asked Sep 03 '17 05:09

Caesar


People also ask

What does decltype stand for?

Software Engineering C++ Decltype stands for declared type of an entity or the type of an expression. It lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.

What does decltype do in C++?

The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments.

What does decltype auto do?

decltype(auto) is primarily useful for deducing the return type of forwarding functions and similar wrappers, where you want the type to exactly “track” some expression you're invoking.

Is decltype runtime or compile time?

decltype is a compile time evaluation (like sizeof ), and so can only use the static type.


3 Answers

The standard seems ambiguous im this area.

An entity is a value, object, reference, function, enumerator, type, class member, bit-field, template, template specialization, namespace, or parameter pack.

The expression a->x can be said to name a member x of struct A, which has type double. The same expression can also be said to name an object which has type const double. Both of these things are entities. The normative text doesn't make it absolutely clear that the intended interpretation is the first one, it can only be inferred from the example.

like image 194
n. 1.8e9-where's-my-share m. Avatar answered Oct 24 '22 02:10

n. 1.8e9-where's-my-share m.


N4687 [dcl.type.simple] ¶4.2 ...if e is an unparenthesized id-expression or an unparenthesized class member access, decltype(e) is the type of the entity named by e.

A class member access is either . or ->, according to [expr.ref].

[basic] ¶3 An entity is a value, object, reference, function, enumerator, type, class member, bit-field, template, template specialization, namespace, or parameter pack.

¶4 A name is a use of an identifier, operator-function-id, literal-operator-id, conversionfunction-id, or template-id that denotes an entity or label.

¶5 Every name that denotes an entity is introduced by a declaration.

There is an ambiguity here: a->x is both a class member and an object (a member subobject). The important thing to note is that decltype(e) is the type of the entity named by e. The only kinds of entities that can be named are those that are introduced by declarations (¶5). A member subobject does not have a name in this sense, as it is not declared. That leaves the only other alternative that decltype(x->a) must be the type of the class member (not the object member).

like image 4
Oktalist Avatar answered Oct 24 '22 01:10

Oktalist


The "entity" named by a class member access expression is that class member, in this case, A::x.

The type of A::x is double.

like image 1
T.C. Avatar answered Oct 24 '22 00:10

T.C.