What is the point of the 'auto' keyword?

后端 未结 8 883
暗喜
暗喜 2020-12-16 03:30

So I understand using var in C# makes sense because you have anonymous types that are compiler derived. C++ doesn\'t seem to have this feature (unless I\'m wron

相关标签:
8条回答
  • 2020-12-16 03:53

    In C++, auto keyword provides a way of type deduction mechanism. For example,

       auto i = expressions;
    

    auto keyword tells the compiler to determine the type of variable i from the expression on the right side of the assignment operator.

    Therefore if the value of expressions is double, then variable i will be double. Or, if the value of expressions is bool, then variable i will be bool.

    0 讨论(0)
  • 2020-12-16 03:54

    syntactic sugar

    I rather say

    auto i = mapping.begin();
    

    over

    std::map<int, int>::iterator i = mapping.begin();
    
    0 讨论(0)
  • 2020-12-16 03:57

    C++ does have "anonymous" types - types you cannot refer to by name because the name is not available to you. This was the case even before C++11 and lambdas. Consider the following code:

    class foo {
        class bar { 
          public:
            void baz() { }
        };
      public:        
        static bar func() { return bar(); }
    };
    
    foo::func().baz(); // OK, only the name "bar" is private
    ??? a = foo::func(); // Umm...
    auto b = foo::func(); b.baz(); // Hooray!
    

    Even if not actually declared in a private scope, it is often useful for a library to leave some types unspecified in its API - especially when heavily utilizing expression templates or other template metaprogramming where the type names can be arbitrarily long with all the nested template arguments. Even the standard itself does this - for instance, the result type of std::bind is not defined by the specification.

    0 讨论(0)
  • 2020-12-16 04:00

    It is well worth reading Herb Sutter's article Almost Always Auto for some great examples of why it's worth using auto over explicit types. The main advantages are the reduction in typing, and gives additional safety if the underlying types change. One of my favourite examples though is about how it reduces duplication. If you allocate on the stack then you'd use:

    MyClass c(param);
    

    However, if you want to create on the heap you need:

    MyClass* c=new MyClass(param);
    

    So you've had to duplicate the MyClass, but the RHS already forces the variable to be a MyClass pointer, so you can just use this instead:

    auto c=new MyClass(param);
    

    If you want to declare it as a unique_ptr then previously you would need:

    unique_ptr<MyClass> c=make_unique<MyClass>(param);
    

    which can be abbreviated to:

    auto c=make_unique<MyClass>(param);
    
    0 讨论(0)
  • 2020-12-16 04:04

    so, let's learn type inference first which is basically refers to automatic deduction of the data type of an expression in a programming language.

    before C++ 11 all the variables in c++ have to explicitly declare but after the release of c++ 11, the compiler itself deduces the type of the variable at runtime.

    we can use it for variables and even in the case of function return types. but, it's suggested to avoid using auto in function return type.

    0 讨论(0)
  • 2020-12-16 04:06

    auto has a lot of uses when it comes down to both generic programming and to save the programmer some typing.

    For example, consider this. Would you rather type out:

    std::unique_ptr<name::long_type::goes_here> g = 
        std::make_unique<name::long_type::goes_here>(1,2,3,4)
    

    or:

    auto g = std::make_unique<name::long_type::goes_here>(1,2,3,4)
    

    Yes, they're both long but we know the return type and specifying it again is a bit cumbersome to type. This also goes for iterators:

    for(auto i = vec.begin(); ...)
    

    vs:

    for(std::vector<type>::iterator i = vev.begin(); ...)
    

    Its use in generic programming is also to figure out the return type of a function or if you're doing some generic algorithms where you don't know the type.

    For example, consider a very basic example.

    template<typename T, typename U>
    auto add(T t, U u) -> decltype(t + u) {
        return t + u;
    }
    

    This allows the compiler to figure out the type of the add operation rather than us trying to figure it out ourselves. Note that in C++14 you can omit the trailing return type. Its uses in generic programming don't stop there either. If we wanted to work with any type of container as a wrapper function for algorithms we could use auto to help us with it. For example:

    template<class Cont>
    void my_sort(Cont&& cont) {
        using std::begin;
        auto first = begin(std::forward<Cont>(cont));
        // work with the iterators here
    }
    

    In the future (C++14), auto can be used to make polymorphic lambdas as well such as:

    [](auto a) { return a + 4; }
    

    Which can be useful as well.

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