Site icon Learn C++

What Is Decltype (auto) In Modern C++ And How To Use It?

The auto keyword arrives with the new features of the C++11 and the standards above. In C++14, there is a new decltype that is used with auto keyword. In modern C++, the decltype(auto) type-specifier deduces return types while keeping their references and cv-qualifiers, while auto does not. In this post, we explain what decltype (auto) is in modern C++ and how to use it.

What is auto in modern C++?

The auto keyword is used to define variable types automatically, it is a placeholder type specifier (an auto-typed variable), or it can be used in a function declaration, or a structured binding declaration. If you want to learn more about auto keyword, here it is,

What is decltype in modern C++?

The decltype keyword and operator represents the type of a given entity or expression. This feature is one of the C++11 features added to compilers (including BCC32 and other CLANG compilers). In a way you are saying “I am declaring this variable to be the same type as this other variable“. Here are more details about how you can use it,

How to use decltype (auto) in modern C++?

In C++14, there is a new decltype feature that allows you to use with the auto keyword. In C++14 and standards above, the decltype(auto) type-specifier deduces return types while keeping their references and cv-qualifiers, while auto does not.

Since C++14, here is the syntax,

[crayon-6646e3c0cd961842304268/]

In this syntax, the type is decltype(expr) and expr can be an initializer or a return statement.

Here is a simple example how we can use it,

[crayon-6646e3c0cd968163042820/]

Are there some simple examples about decltype (auto) in modern C++?

Here are some simple examples that shows difference between auto and decltype(auto),

In C++14 and above, we can use decltype(auto) with const int values as below,

[crayon-6646e3c0cd96a545724289/]

In C++14 and above, we can use decltype(auto) with int& values as below,

[crayon-6646e3c0cd96b829959966/]

In C++14 and above, we can use decltype(auto) with int values as below,

[crayon-6646e3c0cd96d662912952/]

In C++11 and above, we can use auto for return types,

[crayon-6646e3c0cd972526154630/]

In C++14 and above, we can use decltype(auto) for return types,

[crayon-6646e3c0cd974711505794/]

Is there a full example about decltype (auto) in modern C++?

Here is a full example that shows how you can use auto and decltype(auto) in different int types.

[crayon-6646e3c0cd976197927230/]

Here is the output:

[crayon-6646e3c0cd978575954374/]

Note that, as in these examples, you can use other types like, long int, unsigned long int, long long int, float, double, etc.

For more information about this decltype(auto) feature, please see papers below,

C++ Builder is the easiest and fastest C and C++ IDE for building everything from simple to professional applications. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here

Exit mobile version