C++C++14C++17C++20Learn 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,

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,

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,

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

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

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

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

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.

Here is the output:

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,

What Is Decltype auto In Modern C++ And How To Use It The C++ Builder Logo

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?