Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is decltype and how is it used?

I haven't been able to find a good explanation of decltype. Please tell me, as a beginning programmer, what it does and why it is useful.

For example, I am reading a book that asked the following question. Can someone explain to me the answer and why, along with some good (beginner-level) examples?

What would be the type of each variable and what value would each variable have when the code finishes?

int a = 3, b = 4;     decltype(a) c = a;  decltype((b)) d = a;   ++c;   ++d; 

A line-by-line explanation would be very helpful.

like image 912
Olpers Avatar asked Sep 15 '13 17:09

Olpers


People also ask

What does decltype stand for?

Decltype keyword in 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. SYNTAX : decltype( expression )

What does decltype function 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 is the difference between auto and decltype Auto?

auto is a keyword in C++11 and later that is used for automatic type deduction. The decltype type specifier yields the type of a specified expression. Unlike auto that deduces types based on values being assigned to the variable, decltype deduces the type from an expression passed to it.

What does decltype return?

decltype returnsIf what we pass to decltype is the name of a variable (e.g. decltype(x) above) or function or denotes a member of an object ( decltype x.i ), then the result is the type of whatever this refers to. As the example of decltype(y) above shows, this includes reference, const and volatile specifiers.


1 Answers

decltype is a way to specify a type: You give it an expression, and decltype gives you back a type which corresponds to the type of the expression. Specifically, decltype(e) is the following type:

  • If e is the name of a variable, i.e. an "id-expression", then the resulting type is the type of the variable.

  • Otherwise, if e evaluates to an lvalue of type T, then the resulting type is T &, and if e evaluates to an rvalue of type T, then the resulting type is T.

Combining these rules with reference collapsing rules allows you to make sense of decltype(e) &&, which is always a "suitable" reference. (C++14 also adds decltype(auto) to give you the type-deduction of auto combined with the value category semantics of decltype.)

Examples:

int foo(); int n = 10;  decltype(n) a = 20;             // a is an "int" [id-expression]  decltype((n)) b = a;            // b is an "int &" [(n) is an lvalue]  decltype(foo()) c = foo();      // c is an "int" [rvalue]  decltype(foo()) && r1 = foo();  // int && decltype((n)) && r2 = n;        // int & [& && collapses to &] 

It might be worth stressing the difference between auto and decltype: auto works on types, and decltype works on expressions.

You shouldn't be seeing or using decltype in "day-to-day" programming. It is most useful in generic (templated) library code, where the expression in question is not known and depends on a paramater. (By contrast, auto may be used generously all over the place.) In short, if you're new to programming, you probably won't need to use decltype for some time.

like image 172
Kerrek SB Avatar answered Oct 10 '22 09:10

Kerrek SB