Decltype specifier assignment

I am reading a proposal for a qualified search for a name. From there is a quote:

If the resolution operator for the scope :: scope in the subclass specifier is not preceded by the decltype specifier, the search for the name preceding this :: considers only namespaces, types and templates, specializations are types.

As defined in the decltype-specifier standard:

 decltype-specifier: decltype ( expression ) decltype ( auto ) 

What does it mean? Can you explain the purpose of this keyword?

+6
source share
1 answer

decltype is one of the new keywords introduced with C ++ 11. It is a specifier that returns the type of expression .

In template programming, it is especially useful to get an expression type that depends on template parameters or return types.

Examples from the documentation :

 struct A { double x; }; const A* a = new A{0}; decltype( a->x ) x3; // type of x3 is double (declared type) decltype((a->x)) x4 = x3; // type of x4 is const double& (lvalue expression) template <class T, class U> auto add(T t, U u) -> decltype(t + u); // return type depends on template parameters 

As for the second version of the specifier, in C ++ 14 it will be allowed to read several tedious decltype declarations:

 decltype(longAndComplexInitializingExpression) var = longAndComplexInitializingExpression; // C++11 decltype(auto) var = longAndComplexInitializingExpression; // C++14 

Edit:

decltype can naturally be used with the scope operator.

An example from this existing post :

 struct Foo { static const int i = 9; }; Foo f; int x = decltype(f)::i; 

Your quote from the standard indicates that in this case, the name lookup for decltype(f) does not take into account only namespaces, types, and patterns whose specialization is types. This is due to the fact that in this case, the name search is passed to the decltype operator decltype .

+10
source

Source: https://habr.com/ru/post/970152/


All Articles