For Auto in C++
Introduction
The original and updated meanings of this keyword are specified in the C++ standard. Prior to Visual Studio 2010, the "auto" keyword declared a local lifetime variable that is in the automatic storage class. The auto keyword defines a variable whose type is determined by the initialization statement in its declaration as of Visual Studio 2010 onward. The auto keyword's meaning is controlled by the /Zc:auto[-] compiler option.
Syntax:
auto declarator initializer ;
[](auto parameter1 , auto parameter2 ) {};
Remarks:
The “auto” keyword instructs the compiler to determine the type of a lambda expression parameter or declared variable from its initialization expression.
Unless you truly desire a conversion, we advise using the auto keyword because it offers the following advantages:
- Robustness: It simply functions even when the type of the expression changes, including when the type of a function returns changes.
- Performance: There won't be any conversion, you can count on it.
- Usability: You do not have to be concerned about typos or name-spelling errors.
- Efficiency: There is room for improvement in your coding.
Cases of conversion where using auto might not be a good idea include:
- Nothing less than the desired type will do.
- Using the auxiliary types for expressions, for instance, (valarray+valarray).
When declaring a variable, the auto keyword should be used in place of the type and an initialization phrase. Additionally, specifiers and declarators like pointer (*), const, reference (&), rvalue reference (&&) and volatile can be used to change the auto keyword (&&). The initialization expression is evaluated by the compiler, which then makes use of the results to determine the type of the variable.
There are various variations of the auto initialization expression:
- The syntax for universal initialization, such as auto a { 42 };
- the syntax for assignments, like auto x = 0;
- The two preceding versions are combined in universal assignment syntax, such as auto c = 3.14159;.
- Initialization is done directly or using constructor-style syntax, like auto d(1.41421f);.
A distinct initialization syntax is used when the auto is utilized to define the loop parameter in a range based on a statement, such as for (auto& I iterable) do action(i);.
The auto keyword serves as a stand-in for a type but is not a type in and of itself. As a result, the auto keyword cannot be used with operators like sizeof or (for C++/CLI) typeid or with casts.
Usefulness:
A straightforward method to define a variable with a complex type is to use the auto keyword. When the initialization statement contains pointers to functions, templates, or pointers to members, for instance, you can define a variable using auto.
A variable can be declared and initialized to a lambda expression using auto as well. Since only the compiler is aware of the type of a lambda expression, you cannot define the type of the variable yourself.
Types of Trailing Returns
To assist in creating template libraries, utilize auto and the decltype type specifier. Declare a function template whose return type depends on the types of its template arguments using auto and decltype. Alternately, you can declare a function template using auto and decltype that wraps a call to some other function and returns whatever that other function's return type is.
CV Qualifications and References
Utilizing volatile qualifiers, const qualifiers, and auto drops references. Think about the following instance:
Code
#include <iostream>
using namespace std;
int main( )
{
int c = 10;
int&count_Ref = c;
auto my_Auto = count_Ref;
count_Ref = 11;
cout<< c << " ";
my_Auto = 12;
cout<< c <<endl;
}
Output:
[Running] cd "d:\Programming\C++\" && g++ test.cpp -o test && "d:\Programming\C++\"test
11 11
[Done] exited with code=0 in 0.773 seconds
Because my_Auto is an "int", not an int reference, the output in the previous example is 11 11, rather than 11 12, as it would have been if auto hadn't lost the reference qualifier.
Braced Initializers and Type Deduction (C++14)
Braces are used to initialize an auto variable in the following piece of code. Keep in mind the difference between b and c as well as a and e.
Code:
#include <initializer_list>
int main()
{
auto a = { 2, 3 }; // std::initializer_list<int>
auto b = { 4 }; // std::initializer_list<int>
auto c{ 5 }; // int
// C3535: 'auto' type cannot be determined from the initializer list'
auto d = { 6, 7.7 };
// The type for "auto" in C3518 can only be determined from a single initializer phrase in a context including direct-list initialization.
auto e{ 9, 10 };
return 0;
}
Output:
[Running] cd "d:\Programming\C++\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile&& "d:\Programming\C++\"tempCodeRunnerFile
tempCodeRunnerFile.cpp: In function 'int main()':
tempCodeRunnerFile.cpp:12:23: error: unable to deduce 'std::initializer_list<auto>' from '{6, 7.7000000000000002e+0}'
auto d = { 6, 7.7 };
^
tempCodeRunnerFile.cpp:12:23: note: deduced conflicting types for parameter 'auto' ('int' and 'double')
tempCodeRunnerFile.cpp:15:19: error: direct-list-initialization of 'auto' requires exactly one element [-fpermissive]
auto e{ 9, 10 };
^
tempCodeRunnerFile.cpp:15:19: note: for deduction to 'std::initializer_list', use copy-list-initialization (i.e. add '=' before the '{')
[Done] exited with code=1 in 0.157 seconds
Error Messages and Restrictions
The following table details the limitations on the usage of the auto keyword as well as the diagnostic error message the compiler generates when those limitations are met.
Error number | Description |
C3530 | Any other type of specifier cannot be used with the “auto” keyword. |
C3531 | An initializer is required for symbols that are specified using the “auto” keyword. |
C3532 | You erroneously declared a type using the “auto” keyword. You might have declared an array or a method return type, for instance. |
C3533, C3539 | The “auto” keyword cannot be used to declare either a parameter or a template argument. |
C3534 | The “auto” keyword cannot be used to declare a method or template parameter. |
C3535 | A symbol must first be initialized before it can be utilized. The practical implication is that a variable cannot be used to initialize itself. |
C3536 | A type that is specified with the “auto” keyword cannot be cast. |
C3538 | A declarator list with the “auto” keyword defined must resolve each symbol to the same type. |
C3540, C3541 | A symbol that has the “auto” keyword in its declaration cannot be subjected to the "sizeof" and "typeid" operators. |
Examples:
These bits of code serve as examples of several possible uses for the auto keyword.
The declarations that follow are interchangeable. The type of variable i is specified to be int in the first sentence. Because initialization expression (0) is an integer, it can be assumed that variable j in the second sentence is of type int.
Code:
// The explicit type of variable i is int.
int i = 0;
// Because zero is an integer, the variable jhas the implicit type of int.
auto j = 0;
The declarations that follow are identical, but the second one is easier to understand. The convenience of using the “auto” keyword is among its most appealing arguments.
Code:
map<int,list<string>>::iterator k = m.begin();
auto k = m.begin();
When the "for" and range for loops begin, the following code piece declares the types of the variables itr and elm.
Code:
#include <deque>
using namespace std;
int main()
{
deque<double>dq_Double_Data(10, 0.1);
for (auto itr = dq_Double_Data.begin(); itr != dq_Double_Data.end(); ++itr)
{ /* ... */ }
// Keeping the following information in mind, choose range-for loops
//(Any range-for with auto is affected by this, not just deque)
// COPIES elements, which is only somewhat better than the other examples.
for (auto elm :dq_Double_Data)
{ /* ... */ }
// observes elements IN-PLACE and/or adjusts them
for (auto&elm :dq_Double_Data)
{ /* ... */ }
// examines elements IN-PLACE
for (const auto&elm :dq_Double_Data)
{ /* ... */ }
}
The “new” operator and a pointer declaration are used to declare pointers in the following code segment.
Code:
double a = 12.34;
auto *b = new auto(a), **c = new auto(&b);
The following snippet of code includes several symbol declarations in every declaration statement. Keep in mind that each statement's symbols are all resolved to the same type.
Code:
// Resolves to int.
auto a = 1, *b = &a, **c = &b;
// Resolves to double.
auto x(2.01), *y (&a);
// Resolves to char.
auto z = 'x', *p(&z);
// Resolves to int.
auto n = 1, &m = n;
The conditional operator (?:) is used in the following code fragment to declare variable x as an integer with the value 300:
Code:
int v_1 = 100, v_2 = 300;
auto x = v_1 > v_2 ? v_1 : v_2;
The code snippet that follows initializes the variables a to be of type int, b to be a reference to a variable of type const int, and fun_p to be a pointer to the function that returns a variable of type int.
Code:
int fun(int a)
{ return a; }
int main()
{
auto a = fun(0);
const auto& b = fun(1);
int (*ptr)(int a);
ptr = fun;
auto fun_p = ptr;
}