Objective
C + + is a strongly typed language, and you must explicitly indicate its type when declaring a variable. However, in practice, it is difficult to infer the type of the value of an expression, especially with the appearance of the template type, which makes it more difficult to understand the return type of some complex expressions. To solve this problem, there are two main uses of auto in c++11: Automatic type inference and the return value placeholder. The semantics of the temporary variable that the auto is identifying in c++98 has been removed in c++11 because it is rarely used and is redundant. Before and after the two standard auto, is completely two concepts.
Automatic type inference
Auto Auto type inference, which is used to infer the data type of a variable from an initialization expression. Auto type inference can greatly simplify our programming work. Here are some examples of using auto.
#include <vector>
#include <map>
using namespace std;
int main (int argc, char *argv[], char *env[])
{
//auto A; Error, no initialization expression, cannot infer the type of a
//auto int a = 10;//error, the semantics of the auto temporary variable no longer exists in the c++11, which is the use of the old standard.
//1. Auto Help derivation type
auto a = ten;
Auto C = ' A ';
Auto S ("Hello");
2. Type
of lengthy map<int, map<int,int> > map_;
Map<int, Map<int,int>>::const_iterator itr1 = Map_.begin ();
Const Auto ITR2 = Map_.begin ();
Auto ptr = [] ()
{
std::cout << "Hello World" << Std::endl;
};
return 0;
};
3. When using template technology, if the type of a variable depends on the template parameter,
//No auto will make it difficult to determine the type of the variable (automatically determined by the compiler when you use Auto).
Template <class T, class u>
void Multiply (T T, u u)
{
Auto V = t * u;
Second, the return value occupies a position
Template <typename T1, typename t2>
Auto Compose (T1 T1, T2 T2)-> decltype (t1 + T2)
{return
t1+t2;
}
Third, the use of precautions
1, we can use,, valatile
pointer(*)
reference(&)
, rvalue reference(&&)
to modify the auto
Auto k = 5;
auto* PK = new auto (k);
auto** PpK = new Auto (&k);
2. Variables declared with auto must be initialized
3, auto can not be used with other types of combination
4. Function and template parameters cannot be declared as auto
void MyFunction (auto parameter) {}/No auto as method argument
Template<auto t>//utter nonsense-not allowed
5, defined on the heap variables, the use of auto expression must be initialized
int* p = new Auto (0); Fine
int* pp = new auto ();//should be initialized
auto x = new auto ()//hmmm ... no intializer
auto* y = New Auto (9); Fine. Here y is a int*
6, that auto is a placeholder, is not a type of his own, so can not be used for type conversion or some other operations, such as sizeof and typeid
int value = 123;
Auto x2 = (auto) value; No casting using auto
7. Variables defined in an auto sequence must always be deduced to the same type
8. Auto cannot be automatically deduced into Cv-qualifiers (constant & volatile qualifiers) unless declared as a reference type
const int i =;
Auto J = i; j is int, rather than const int
j = m//Fine. As J isn't constant
//Now let us try to have reference
auto& k = i;/now K is const int&
k = 100; Error. K is constant
9, auto will degenerate exponentially to the array of pointers, unless it is declared as a reference
int a[9];
Auto J = A;
Cout<<typeid (j). Name () <<endl; This'll print int*
auto& k = A;
Summarize
The above is the entire content of this article, I hope the content of this article for everyone to learn or use C + + can have some help, if there is doubt you can message exchange.