Site icon Learn C++

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:

[crayon-676396339443c474453103/]

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.

[crayon-6763963394444640876994/]

here is more detailed example:

[crayon-6763963394447980852588/]

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.

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.

[crayon-6763963394448823863225/]

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

[crayon-676396339444a314069948/]

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

Exit mobile version