Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ 11 feature: auto keyword     - Oracle Client Dedicated and Shared connection mode (Database)

- Oracle delete table space prompted ORA-00604 and ORA-38301 (Database)

- Ubuntu installed racing game Speed Dreams 2.1 (Linux)

- Dom4j change XML coding (Programming)

- Linux, modify / etc / profile file (Linux)

- How to install MySQL on Linux Dock (Database)

- Linux linux system security (Linux)

- HTML5 Application Cache (Programming)

- Struts2 interceptor simulation (Programming)

- How do you prevent other users from accessing your home directory in Linux (Linux)

- 12 novice Linux command must learn (Linux)

- Oracle table space create large files (Database)

- PostgreSQL using the system cache to improve operational efficiency (Database)

- grep regular expression (Linux)

- Ubuntu 15.04 using the Eclipse 4.4, Java 8 and WTP (Linux)

- C ++ Learning Notes: references (Programming)

- Ubuntu Linux to create and increase the Swap partition tutorial (Linux)

- Linux see whether there is a hacker program (Linux)

- Python context managers (Programming)

- Nginx start, stop, smooth start, smooth upgrade (Server)

 
         
  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:      
 
- MySQL replication master and slave structure built (Database)
- Configure shared library PCRE (Linux)
- Machine Learning: Classification of the curse of dimensionality (Programming)
- installation of Piwik under Ubuntu (Programming)
- How to upgrade to Ubuntu 14.04 Ubuntu 14.10 (Linux)
- Linux find command detailing (Linux)
- CentOS 6.5 installation configuration DRBD (Server)
- Ubuntu 15.10 How to install TeamViewer 11 (Linux)
- Doubly linked list basic operations (Linux)
- Camera-based face recognition OpenCV crawl and storage format (Python) (Linux)
- CentOS 6.4 under PXE + Kickstart unattended operating system installation (Programming)
- MySQL master-slave database configuration and error handling Raiders (Database)
- MySQL development common query summary (Database)
- HBase Application Development Review and Summary of Series (Database)
- Oracle 11g user rights management study notes (Database)
- VPS xen openvz kvm (Server)
- Dockerfile use to build a mirror-based CentOS 7 (Linux)
- Infinispan 8 new Redis cache storage implementation (Linux)
- TeamCity continuous integration in the Linux installation (Linux)
- Linux System Getting Started Learning: The Linux logrotate (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.