Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ 11 feature: auto keyword     - To configure linux transparent firewall (Linux)

- Linux Network Programming - non-blocking program (Programming)

- Repair Raspbian Encountered a section with no Package (Linux)

- GNU / Linux enable Intel Rapid Start (Linux)

- The difference between Linux su and sudo commands (Linux)

- Linux installation JDK1.6 rpm.bin assembly (Linux)

- Linux nice program origin of the name (Linux)

- Java proxy mode (Programming)

- OpenCV cvFindCornerSubPix () to find sub-pixel Corner (Programming)

- Select helps secure the system network management tools (Linux)

- Linux SSH commands (Linux)

- How to install Visual Studio Code on Ubuntu (Linux)

- Linux server alarms using Java (Server)

- Configure the ASM process on Red Hat Linux 6.5 (Database)

- Getting Started with Linux system to learn: how to get the process ID (PID) in the script (Linux)

- Talk about the Linux folder permissions issue again (Linux)

- Management DB2 logs (Database)

- Installation and Configuration Tomcat environment CentOS 6.6 (Server)

- Denyhosts prevent hackers using SSH scanning (Linux)

- Linux boot the system does not display a progress bar setting method (Linux)

 
         
  C ++ 11 feature: auto keyword
     
  Add Date : 2018-11-21      
         
         
         
  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
     
         
         
         
  More:      
 
- How apt-get limited use IPv4 or IPv6 protocol to download (Linux)
- Linux kernel netpoll framework netconsole (Linux)
- Linux server alarms using Java (Server)
- Fun music library in Linux using command line (Linux)
- Python Django direct implementation of sql statement (Programming)
- Solaris 11 forget the root password (Linux)
- ORA-28000 the account is locked fault simulation (Database)
- Additional SQL Server 5123 database reported error (Database)
- In Debian 4.3 compiler under Linux-2.6.28 kernel Summary (Programming)
- Repair Chrome for Linux is (Linux)
- Linux shell scripts bubble sort (Programming)
- Radius server setup under CentOS (Server)
- Linux environment has been running Tomcat how to deploy the new Tomcat (Server)
- The principle Httpclient4.4 (execution request) (Programming)
- How to use the Linux terminal Git commands (Linux)
- Security Features Linux and Unix operating system, programming (Linux)
- Elasticsearch Kibana installation notes (Linux)
- How comprehensive resist attacks from the network (Linux)
- Java reflection technology explain (Programming)
- Seven kinds of NIC binding mode Detail (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.