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.
Table of Contents
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,
1 2 3 |
type_constraint (optional) decltype ( auto ) |
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,
1 2 3 |
decltype(auto) x = i; |
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,
1 2 3 4 5 |
const int x = 4096; auto xa = x; // xa : int decltype(auto) xb = x; // xb : const int |
In C++14 and above, we can use decltype(auto)
with int&
values as below,
1 2 3 4 5 6 |
int y = 2048; int& y0 = y; // y_ : int auto ya = y0; // yc2 : int decltype(auto) yb = y0; // yb : int& |
In C++14 and above, we can use decltype(auto)
with int
values as below,
1 2 3 4 5 |
int&& z = 1024; auto zm = std::move(z); // zm : int decltype(auto) zm2 = std::move(z); // zm2 : int&& |
In C++11 and above, we can use auto
for return types,
1 2 3 4 5 6 |
auto myf(const int& i) { return i; // auto return type : int } |
In C++14 and above, we can use decltype(auto)
for return types,
1 2 3 4 5 6 |
decltype(auto) myf2(const int& i) { return i; // decltype(auto) return type : const int& } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <iostream> auto myf(const int& i) { return i; // auto return type : int } decltype(auto) myf2(const int& i) { return i; // decltype(auto) return type : const int& } int main() { const int x = 4096; auto xa = x; // xa : int decltype(auto) xb = x; // xb : const int int y = 2048; int& y0 = y; // y_ : int auto ya = y0; // yc2 : int decltype(auto) yb = y0; // yb : int& int&& z = 1024; auto zm = std::move(z); // zm : int decltype(auto) zm2 = std::move(z); // zm2 : int&& int f = myf(x); int f2 = myf2(x); std::cout << "x:" << x << std::endl; std::cout << "xa:" << xa << std::endl; std::cout << "xb:" << xb << std::endl; std::cout << "y:" << y << std::endl; std::cout << "y0:" << y0 << std::endl; std::cout << "ya:" << ya << std::endl; std::cout << "yb:" << yb << std::endl; std::cout << "z:" << z << std::endl; std::cout << "zm:" << zm << std::endl; std::cout << "zm2:" << zm2 << std::endl; std::cout << "myf:" << myf(x) << std::endl; std::cout << "myf2:" << myf2(x) << std::endl; system("pause"); return 0; } |
Here is the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
x:4096 xa:4096 xb:4096 y:2048 y0:2048 ya:2048 yb:2048 z:1024 zm:1024 zm2:1024 myf:4096 myf2:4096 Press any key to continue . . . |
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,
- https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1478.pdf (Note: PDF)
- https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1607.pdf (Note: PDF)
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