Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ C ++ 11 feature: auto keyword     - to compile FFmpeg In Ubuntu (Linux)

- How to properly set up a Linux swap partition (Linux)

- Linux LVM Logical Volume Management to resize partitions (Linux)

- Linux find command usage practices (Linux)

- C # and JavaScript arrays to re-summary (Programming)

- Flask installation environment (Linux)

- To install and deploy PHP environment under the CentOS (Server)

- RT-11SJ run at ambient PDP-11 MACRO-11 assembly (Programming)

- Python calls the API interface in several ways (Programming)

- Java Builder mode (Programming)

- Easy to install Ubuntu 15.04 and Ubuntu 15.04 GNOME on Wayland trial (Linux)

- Install apr support for Tomcat on Linux (Server)

- Radius server setup under CentOS (Server)

- Depth understanding of JavaScript new mechanism (Programming)

- Oracle GoldenGate tutorial Second, configure and use (Database)

- Linux character device - user mode and kernel mode data transfer data (Linux)

- Cobbler remotely install CentOS system (Linux)

- Learning the Linux powerful network management capabilities (Linux)

- Cross server / client backup command: rsync use (Server)

- How VMware extend disk without rebooting the system (Linux)

  C ++ 11 feature: auto keyword
  Add Date : 2018-11-21      

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


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.


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
- Upgrade installation manual CentOS6.5 GCC4.8.2 (Linux)
- Linux IO redirection (Linux)
- Create, modify, delete users, user groups under linux (Linux)
- Linux 10 useful examples of command-line completion (Linux)
- How to install and use the Snort in Ubuntu 15.04 (Linux)
- RHEL7.0 configuration CD-ROM to the local YUM (Linux)
- Use source packages compiled and installed GCC5.1 in Mac OS X 10.10.3 (Linux)
- MySQL master-slave database configuration and error handling Raiders (Database)
- Mac OS X 10.10 Yosemite compiling OpenJDK 9 (Linux)
- How to install Bugzilla 4.4 on Ubuntu / CentOS 6.x (Linux)
- How do you change the default browser and Email Client in Ubuntu (Linux)
- HTML5 Application Cache (Programming)
- Java eight new features 8 (Programming)
- Docker: Mirroring and container operations (Server)
- Windows 8.1 and Ubuntu 14.04 dual system uninstall Ubuntu Tutorial (Linux)
- Oracle SQL statement tracking (Database)
- Tip: Use Cryptsetup U disk encryption (Linux)
- Why do you need close contact Rust 1.0 (Programming)
- The direct insertion sort algorithm (Programming)
- Repair after installing Ubuntu no boot device error (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.