What is the simplest example using auto in C++?

10,367

Solution 1

auto is used for type inference, i.e. to declare a type based on the expression, instead of explicitly stating it.

auto val = 3; // here, val is declared as an integer 

Obviously this isn't such a big advantage over int val = 3 so let's make a better example:

std::vector<int> container; // ...
for (auto it = container.begin(); it != container.end(); it++) {
    // type of it is std::vector<int>::iterator, but you don't need to state that
}

here you don't have to care about the real container type, i.e. std::vector<int>::iterator it = c.begin(). Additionally, you can change container from vector to list, and the loop would still work without any changes as type of it would be correctly deduced.

NOTE: in C++11 you would write the loop above as for( auto it: container) but this serves better as an illustration.

Solution 2

auto is useful to avoid typing verbose names, which is what you usually get with templates. But in general, the pattern goes like this:

struct my_class_with_a_long_name { };

my_class_with_a_long_name foo()
{
    my_class_with_a_long_name obj;
    // ...
    return obj;
}

int main()
{
    auto myObj = foo();
}

But it's not just about typing less characters, it is also about consistency. If you change the return type of foo, you won't have to explicitly change also the type of myObj.

Basically, the point is that when the type of a certain expression is known to the compiler at compile-time, there is no reason for not exploiting this knowledge and letting the compiler use it automatically, in whatever context this can be done.

The only situation where you likely do not want to use auto is when you want to perform conversions, or when you want the type of a variable to appear explicit before its declaration for readability reasons.

Solution 3

I assume you know what std::vector<int> is (basically a resizable array of integers).

Let's say you have just such a vector, and want to iterate over its elements. Here, auto comes in very handy:

std::vector<int> items;
...
for (auto iter = std::begin(items); iter != std::end(items); ++iter) {
    // do something with `iter'
}

While iter is conceptually simple (it's an iterator), it has a somewhat complex type (std::vector<int>::iterator). Using auto saves you having to spell out the type. It also makes it easier to change the type of items to some other container.

Share:
10,367

Related videos on Youtube

Roman
Author by

Roman

Updated on September 15, 2022

Comments

  • Roman
    Roman over 1 year

    I try to understand how auto is used in C++. For me the best way to understand something is to see an example. However, the examples that I saw are not so simple. For example here is "Meaning of C++0x auto keyword, by example?". To understand this example I need to know what are "templates", "pointers", "malloc" and so on.

    Can anybody, please, give an minimalistic example using auto so one can easy understand what it is used for?

    • Prasanth Kumar
      Prasanth Kumar about 11 years
      Your question is not actually about "auto". It is about "templates", "pointers", "malloc" and so on. If you don't know what they are, just don't worry about "auto" yet - get a good book to learn from.
    • WhozCraig
      WhozCraig about 11 years
      Difficult to pin down to a specific "this is it" meaning. I can only offer this. It is useful for automagic deduction of "type" from the code context in which it is used. Thats about as "blanket" as I can get.