|
Foreword
Content of this article is not new. About auto, over and over again to be aware of all these things, this paper does not propose new auto usage.
I was originally a hate blog articles are copy from the lack of new ideas to explore, of course, is not a copy of this article from, but we publish such an article and my heart is very well known fear.
In this paper, the contents of the auto sorting out their own right when the review notes.
C ++ 98 auto
Early in C ++ 98 standard existed auto keyword, when the auto is used to declare variable to automatic, automatic with automatic variable meaning of life, this is redundant, because if not using auto declare variables still have automatic lifetime:
int a = 10; // with automatic lifetime
auto int b = 20; // with automatic lifetime
static int c = 30; // extend the life of
C ++ in 98 auto unnecessary and rarely used, C ++ 11 has been removed in this usage, replaced by a new auto: automatic type inference of variables.
C ++ 11 auto
auto can automatically select the matching type for this variable in the variable declaration when the initial value variable depending on the type, there are similar keywords decltype. for example:
int a = 10;
auto au_a = a; // Automatic type inference, au_a type int
cout << typeid (au_a) .name () << endl;
typeid operator can output the type of the variable. Operating results of the output of the program
int
This usage is similar to C # or java in the var keyword. auto automatic type inference occurs at compile time, so the use of auto and will not cause runtime efficiency. And will result in compile time consuming, and I think it is not, and when not in use auto, the compiler needs to know the type of the right operand, and then the type of the left operand is compared to check whether changes accordingly conversion, the need for implicit type conversions.
auto usage
Cited above, this example is very simple, the programming in real time is not recommended to use this type of auto, direct write variables more clear and understandable. Here are the correct usage of auto keyword.
It used instead of long and complex, variable use of specific variable declaration.
Imagine no auto when we often need this standard library operation:
#include < string>
#include < vector>
int main ()
{
std :: vector < std :: string> vs;
for (std :: vector < std :: string> :: iterator i = vs.begin ();! i = vs.end (); i ++)
{
// ...
}
}
So look at the code to write code really fretting. Some might say why not directly use using namespace std, so that the code can be a little shorter. In fact, this is not the recommended method (C ++ Primer relevant to this narrative). Use auto can simplify the code:
#include < string>
#include < vector>
int main ()
{
std :: vector < std :: string> vs;
for (auto i = vs.begin (); i = vs.end ();! i ++)
{
// ..
}
}
for loop i will automatically derive its type at compile time, but we do not have to define explicitly the long string.
When you define a template function, it is used to declare a template parameter dependent variable types.
template < typename _Tx, typename _Ty>
void Multiply (_Tx x, _Ty y)
{
auto v = x * y;
std :: cout << v;
}
Without the use of auto variable declaration v, then this function is difficult to define it, not compile time, who knows what the real type of x * y is it?
The return value is dependent on the template function template parameters
template < typename _Tx, typename _Ty>
auto multiply (_Tx x, _Ty y) -> decltype (_Tx * _Ty)
{
return x * y;
}
When the return value of the function template depends on a template parameter, we still can not determine the type of code before compiling template parameters, it is also no way to know the type of return value, then we can use the auto. , Shown above.
New operator decltype operator for the data type of query expressions, but also the introduction of C ++ 11 standard, which also aims to solve some type of generic programming is determined by the template parameter, but it is difficult to indicate a problem.
Role auto here, also known as the return value of a placeholder, it's just a function returns a value representing the position of the real value is returned back decltype (_Tx * _Ty). Why should the return value of post-it? If there is no rear, the function is declared as:
decltype (_Tx * _Ty) multiply (_Tx x, _Ty y)
By this time _Tx, _Ty not declare it, the compiler can not.
Precautions
auto variable must be initialized in the definition, which is similar to const keyword.
Defined in a variable auto sequence must always be deduced to the same type. E.g:
auto a4 = 10, a5 = 20, a6 = 30; // correct
auto b4 = 10, b5 = 20.0, b6 = 'a'; // error, not derive the same type
When using the auto keyword do automatic type deduction rules applied successively at:
If the initialization expression is a reference to the removal of reference semantics.
int a = 10;
int & b = a;
auto c = b; // c is of type int rather than int & (removal of references)
auto & d = b; // c at this time only for the type int &
c = 100; // a = 10;
d = 100; // a = 100;
If the initialization expression is const or volatile (or both), then remove const / volatile semantics.
const int a1 = 10;
auto b1 = a1; // b1 instead of type int const int (removal const)
const auto c1 = a1; // At this type c1 is const int
b1 = 100; // legal
c1 = 100; // Illegal
If the auto keyword to bring the ampersand is not const semantics removed.
const int a2 = 10;
auto & b2 = a2; // because auto & belt, it is not removed const, b2 type const int
b2 = 10; // Illegal
This is due to how to remove the const, then a2 b2 is a non-const reference, a2 by b2 can change the value, then clearly unreasonable.
Initialization expression is an array, auto key derivation type pointer.
int a3 [3] = {1, 2, 3};
auto b3 = a3;
cout << typeid (b3) .name () << endl;
The program output
int *
If the expression is an array, and auto & belt, the derivation is an array type.
int a7 [3] = {1, 2, 3};
auto & b7 = a7;
cout << typeid (b7) .name () << endl;
Program output
int [3]
Function or template parameters can not be declared as auto
void func (auto a) // error
{
// ...
}
Always pay attention to auto is not really a type.
auto is just a placeholder, it is not a true type, you can not use some type of operands to operators, such as sizeof or typeid.
cout << sizeof (auto) << endl; // error
cout << typeid (auto) .name () << endl; // error |
|
|
|