C++C++11C++14C++17Learn C++Syntax

What Does The decltype Type Specifier Mean In C++?

What Does The decltype Type Specifier Mean In C++

C++ is a highly evolved and mature programming language. The C++ language has a great set of choices of modern C++ tool sets and compilers all of which come with a lot of utilities, GUI components, and libraries. C++11 was a big step for functional programming with C++. It has brought many other features like automatic type deduction (auto), lambda expressions, and decltype features. In this post, we explain what decltype is and how to use it with today’s modern C++.

What is the decltype type specifier in 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 is the Syntax:

Note that, C++11 brings us the C++11 Standard Library and Threading Library with a lot of new features like Lambda expressions, automatic type deduction (auto) and decltype(e), uniform initialization syntax (mapping etc.), deleted and defaulted functions, uniform initialization syntax, Rvalue References, new smart pointer classes, and new C++ algorithms <algorithm>. Most of these features enable C++ to be a functional programming language. Typical C++ functions for use in functional programming include: mapfilter, and reduce. There are also the functions std::transformstd::remove_if, and std::accumulate.

Lambdas are one of the great additions to C++11 and has changed the way we write C++ code. In general, most programmers argue that modern C++ code should be using lambdas because it allows us to create in-place anonymous functions which in turn makes C++ a Functional Programming Language.

Is there a simple example of how to use the decltype type specifier in C++?

It’s easier to explain decltype with an example. Let’s assume there is a variable c and you don’t know its type. You can use decltype. Here is a simple example.

here is more detailed example:

in this example only types of new variables are the same as the old ones. Values of new variables are their default values, values are not copied. In other words it is only used to declare types of that given type.

Is there rules about decltype type specifier in C++ ?

Here are the rules for evaluating decltype.

  • If e is an identifier expression or is accessing a class member, decltype(e) is the type of the identifier or accessor designated by e. If there is no such thing, or if e is the name of a set of overloaded functions, so there is ambiguity, decltype(e)is invalid.
  • Otherwise, if e is a function call or invokes an overloaded operator, decltype(e) is the type returned by the function.
  • Otherwise, if e is an lvalue, decltype(e) is a reference to T (T&), where T is the type of e.
  • If none of the other cases apply, decltype(e) is the type of e.

Here e is a variable that it can be a char, int, float, double, long long int, string, a member of a struct or class object, etc.

Is there a full example of how to use the decltype type specifier in C++?

Here is a simple example that uses decltype to declare new variables.

Here is another full example that uses decltype to define members of other types like functions, structs, classes, etc,

What Does The decltype Type Specifier Mean In C++ The C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. 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++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?