What is decltype and how is it used?

Olpers

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.

Kerrek SB

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What are submodules and how are they used?

From Dev

What is a cookbook? and how is it used?

From Java

What is polymorphism, what is it for, and how is it used?

From Dev

What is rake and how it is used in rails?

From Dev

What is OpenStack? And how can it be used?

From Java

What are some uses of decltype(auto)?

From Dev

What is the type of decltype(this) in C++?

From Dev

What is the type of decltype(this) in C++?

From Dev

How to use decltype in this case

From Dev

How to handle void decltype();

From Dev

How does decltype work?

From Dev

How to test for availability of decltype?

From Dev

How to test for availability of decltype?

From Dev

How to use decltype in this case

From Dev

Get underlying type for a template used in decltype

From Dev

What are shadow registers in MIPS and how are they used?

From Dev

What is a parallel for loop, and how/when should it be used?

From Java

In Flask, What is request.args and how is it used?

From Dev

How to know what icon is being used in the dock?

From Dev

What are static methods? How and when are they used?

From Dev

What are AssemblyKeys used for, and how to import them?

From Dev

What exactly is txPower for Bluetooth LE and how is it used?

From Dev

What is stdin and how is it being used with fscanf?

From Dev

How to know what encryption is used by WebRTC connections?

From Dev

What is LoggingEventData ? How and where should it be used?

From Dev

What is Cometd ? Why it is used and how to work on that

From Dev

What is an AsyncTask (or AsyncResult) and how is it typically used for Android?

From Dev

How to check what the data type is being used?

From Dev

How to know what icon is being used in the dock?

Related Related

HotTag

Archive