Fear_Is_An_Illusion's blog

By Fear_Is_An_Illusion, 10 years ago, In English

The newer versions of C++ have introduced an auto keyword, which basically automatically sets the data type of variable. I can imagine it being useful in situations like these, where it increases coding speed and prevents unnecessary cluttering.


vector< pair <int, int> > foo; for(auto i=foo.begin(),i!=foo.end(),++i) { ..... }

However still it just seems as a small advantage. Also using it in place of int , string, etc just causes a lot of confusion, like dealing with variables in any weakly typed language, say Javascript.

var foo=13.67;

foo="it's a string"

Though using auto prevents assigning a string as an int, but using it everywhere makes debugging a nightmare

Say you shouldnt be doing this in C++

auto x="mewat1000";
auto y=5.33;

So what other useful feature it serves, which I seem to be missing out ?

Upd1 : One of my few posts which didnt get downvoted to oblivion. Rest of the posts disapper from recent blog activity list within 15-20 mins of posting them.

  • Vote: I like it
  • +14
  • Vote: I do not like it

| Write comment?
»
10 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You can feel all power of keyword auto when you use it with range-based loops. Your code can be modified this way.

vector< pair <int, int> > foo;

for(auto i : foo)
{
    .....
}
  • »
    »
    10 years ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    In fact this is not only the power of auto. This is for loop based on range and equivalent to

    for(pair<int,int> i:foo){...}
    

    auto just makes the code shorter.

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I what do you mean by increasing speed?