C++ is a highly evolved and mature programming language. The C++ language has a great set of choices of modern C++ Editor and compilers all of which come with a lot of tools, GUI components, and libraries. C++11 was a big step for functional programming and it brought many other very useful features like automatic type deduction (auto
), lambda expressions, and decltype features. In this post, we explain what is auto
in today’s modern C++ and how to use auto-typed variables.
Table of Contents
What Is auto in C++?
The auto
keyword arrives with the new features in C++11 and C++17 and can be used as a placeholder type specifier (an auto-typed variable), or it can be used in function declaration, or in a structured binding declaration. The auto
keyword can be used with other new CLANG standards like C++14, C++17, etc.
The auto keyword was being used as an automatic data specifier (storage class specifier) until C++11. This feature was removed by the C++11 standard.
How can we use auto in modern C++?
Since C++11 the auto
keyword can be used in 2 different situations,
- As a placeholder type specifier (auto-typed variable)
- In a function declaration
Since C++17 auto
keyword can be used as,
- Structured binding declaration
In this post we will explain auto-typed variables which comes after C++11 and available in other CLANG standards.
What are auto-typed variables in modern C++?
Auto-typed variables are a C++11 feature that allows the programmer to declare a variable of type auto
, with the type itself being deduced from the variable’s initializer expression. The auto
keyword is treated as a simple type specifier (that can be used with *
and &
), and its semantics are deduced from the initializer expression. For more information about this feature, please see the C++11 standard Proposal Document at: auto-typed variables Proposal Document.
The auto
keyword is very useful, for example, it can be used as a placeholder type specifier to declare auto-typed variables.
Here is the syntax.
1 2 3 |
type-constraint(optional) auto <declaration> ; |
Is there a simple auto-typed variables example in modern C++?
Here is a simple example of how to use auto-typed variables in C++.
1 2 3 4 5 |
unsigned long int L; auto a = L; // a is automatically unsigned long int |
The auto
keyword is very useful in lambda declarations as shown below.
1 2 3 |
[](auto p1 , auto p2 ) { }; |
Is there a full example of how to use auto-typed variables in modern C++?
Here are some very simple auto-typed Variables examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int IntFunc() {} bool BoolFunc() {} char* CharSFunc() {} int main() { auto x = IntFunc(); // x is int const auto y = BoolFunc(); // y is const bool auto w = CharSFunc(); // w is char* return 0; } |
Using auto
drops references, const
qualifiers, and volatile
qualifiers, for example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main( ) { int i= 2020; int& ref = i; auto my_auto = ref; ref = 2023; std:: cout << i << std::endl; my_auto = 2024; std::cout << i << endl; system("pause"); return 0; } |
Is there a multi-declarator auto example in C++?
The auto
keyword can be used for multiple variables, and it is referred to as a ‘multi-declarator auto’. The C++11 standard includes the multi-variable form of auto declarations, such as:
1 2 3 4 5 6 7 8 9 10 11 |
int* func() { } int main() { auto x = 3, * y = func(), z = 4; return 0; } |
The restriction with multi-declarator auto expressions is that the variables must have the same base type.
For example, the following line of code is well-formed:
1 2 3 |
auto x = 3, y = new int; |
because x and y have the same base type : int
, while the following code:
1 2 3 |
auto x = 3, y = 3.5; |
will generate the error: [bcc64 Error] File1.cpp(11): 'auto' deduced as 'int' in declaration of 'x' and deduced as 'double' in declaration of 'y'
This feature is supported by the Clang-enhanced C++ compilers. For more information on this feature, please see the C++11 standard Proposal Document at: Multi-declarator auto
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