Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of the 'auto' keyword?

Tags:

c++

c++11

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 wrong), so what is the point of having an auto keyword?

(It is kinda cool that unlike C#, auto does work for member/global variables, which is cool I guess, but doesn't seem enough to justify its existence).

like image 363
sircodesalot Avatar asked Jul 17 '13 01:07

sircodesalot


2 Answers

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.

like image 133
Rapptz Avatar answered Oct 01 '22 14:10

Rapptz


There are a number of uses for auto in C++

  1. Anonymous function objects, aka closures, aka lambda instances. auto is the only way to store them. Types can also be generated derived off those types, and types on their backs, ad infinitum.

  2. C++ can have quite complex types, such as the type of a non mutating iterator into an unordered map that uses a custom allocator and hashing function. typedef can mitigate this, but the type of a m.begin() having a particular name is not that informative: foo_iterator it = is as meaningful as auto foo_iterator =, and the auto one does not require boilerplate elsewhere.

  3. Return type deduction uses the auto keyword, which is required to do some template functions work without huge amounts of traits boilerplate. Eliminating boilerplate is a common theme: C++s robust type system means that types can carry lots of information, and encoding it at every use can be counterproductive.

  4. In some ducktype template code, the work to deduce the type of a variable is roughly the same as the work to code the variables value, and nearly identical in structure, some times literally: decltype(long expression) x = long expression;. auto eliminates that duplication.

  5. Finally in C++1y, type deduction lambdas use auto to say that an argument is a deduced one. Sort of a light weight template. Talk to extend this to non lambdas is also in skunkworks.

like image 26
Yakk - Adam Nevraumont Avatar answered Oct 01 '22 13:10

Yakk - Adam Nevraumont