Is there any reason to use the 'auto' keyword in C++03?

后端 未结 10 1707
[愿得一人]
[愿得一人] 2020-11-28 02:27

Note this question was originally posted in 2009, before C++11 was ratified and before the meaning of the auto keyword was drast

相关标签:
10条回答
  • 2020-11-28 03:13

    In C++11, auto has new meaning: it allows you to automatically deduce the type of a variable.

    Why is that ever useful? Let's consider a basic example:

    std::list<int> a;
    // fill in a
    for (auto it = a.begin(); it != a.end(); ++it) {
      // Do stuff here
    }
    

    The auto there creates an iterator of type std::list<int>::iterator.

    This can make some seriously complex code much easier to read.

    Another example:

    int x, y;
    auto f = [&]{ x += y; };
    f();
    f();
    

    There, the auto deduced the type required to store a lambda expression in a variable. Wikipedia has good coverage on the subject.

    0 讨论(0)
  • 2020-11-28 03:17

    The auto keyword has no purpose at the moment. You're exactly right that it just restates the default storage class of a local variable, the really useful alternative being static.

    It has a brand new meaning in C++0x. That gives you some idea of just how useless it was!

    0 讨论(0)
  • 2020-11-28 03:17

    According to Stroustrup, in "The C Programming Language" (4th Edition, covering C 11), the use of 'auto' has the following major reasons (section 2.2.2) (Stroustrup words are quoted):

    1)

    The definition is in a large scope where we want to make the type clearly visible to readers of our code.

    With 'auto' and its necessary initializer we can know the variable's type in a glance!

    2)

    We want to be explicit about variable's range or precision (e.g., double rather than float)

    In my opinion a case that fits here, is something like this:

       double square(double d)
        {
            return d*d; 
        }
    
        int square(int d)
        {
            return d*d; 
        }
    
        auto a1 = square(3);
    
        cout << a1 << endl;
    
        a1 = square(3.3);
    
        cout << a1 << endl;
    

    3)

    Using 'auto' we avoid redundancy and writing long type names.

    Imagine some long type name from a templatized iterator:

    (code from section 6.3.6.1)

    template<class T> void f1(vector<T>& arg) {
        for (typename vector<T>::iterator p = arg.begin(); p != arg.end();   p)
            *p = 7;
    
        for (auto p = arg.begin(); p != arg.end();   p)
            *p = 7;
    }
    
    0 讨论(0)
  • 2020-11-28 03:20

    Is there some other meaning to 'auto' other than 'local variable?'

    Not in C++03.

    Anything it does that isn't implicitly done for you wherever you may want to use it?

    Nothing whatsoever, in C++03.

    How does an auto variable behave in program scope? What of a static auto variable in file-scope?

    Keyword not allowed outside of a function/method body.

    Does this keyword have any purpose [in C++03] other than just existing for completeness?

    Surprisingly, yes. C++ design criteria included a high degree of backward compatibility with C. C had this keyword and there was no real reason to ban it or redefine its meaning in C++. So, the purpose was one less incompatibility with C.

    Does this keyword have any purpose in C other than just existing for completeness?

    I learned one only recently: ease of porting of ancient programs from B. C evolved from a language called B whose syntax was quite similar to that of C. However, B had no types whatsoever. The only way to declare a variable in B was to specify its storage type (auto or extern). Like this:

    auto i;

    This syntax still works in C and is equivalent to

    int i;

    because in C, the storage class defaults to auto, and the type defaults to int. I guess that every single program that originated in B and was ported to C was literally full of auto variables at that time.

    C++03 no longer allows the C style implicit int, but it preserved the no-longer-exactly-useful auto keyword because unlike the implicit int, it wasn't known to cause any trouble in the syntax of C.

    0 讨论(0)
提交回复
热议问题