tags:

views:

181

answers:

3

I have seen code which use vector,

vector<int>s;
s.push_back(11);
s.push_back(22);
s.push_back(33);
s.push_back(55);
for (vector<int>::iterator it = s.begin(); it!=s.end(); it++) {
    cout << *it << endl;
}

It is same as

for (auto it = s.begin(); it != s.end(); it++) {
    cout << *it << endl;
}

How safe is in this case the use of the auto keyword? And what about if type of vector is float? string?

+3  A: 

The auto keyword gets the type from the expression on the right of =. Therefore it will work with any type, the only requirement is to initialize the auto variable when declaring it so that the compiler can deduce the type.

Examples:

auto a = 0.0f;  // a is float
auto b = std::vector<int>();  // b is std::vector<int>()

MyType foo()  { return MyType(); }

auto c = foo();  // c is MyType
dark_charlie
Though technically correct. I hope this becomes bad practice. If everybody just declares all their variables `auto` it will become hard for humans to read and understand (and we head down the road to untyped languages). The use of auto should be reserved for situations where we don't actually care about the type as long as it behaves in a manor that we want (for example iterators, we don't actually care what iterator we get as long as we can use it like an iterator).
Martin York
@Martin: No kidding, the first time I saw a block of `auto` variables I thought "please die fast." Like you said, it should be used in those sort of "gimme a variable, whatever type it is" situations, not that "declare a variable, and deduce it's type from it's initializer hehehehe!" trying to be tricky crap.
GMan
@Martin: Implicit typing != dynamic typing (I'm guessing that that's what you mean with "untyped"), so `auto x = new int();` `auto y = new float();` `y = x;` stays illegal.The other sense of "untyped" is of course the everything's-a-machine-word of ASM and BCPL. The `reinterpret_cast<>()` loophole is for that ;)
sverkerw
@sverkerw: No. I mean it is hard for humans to read. What is the type of c? Do I need to know. How does it affect my interpretation of the rest of the code.
Martin York
+2  A: 

auto keyword is intended to use in such situation, it is absolutely safe. But unfortunately it available only in C++0x so you will have portability issues with it.

Kirill V. Lyadvinsky
+12  A: 

The auto keyword is simply asking the compiler to deduce the type of the variable from the initialization.

Even a pre-C++0x compiler knows what the type of an (initialization) expression is, and more often than not, you can see that type in error messages.

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int>s;
    s.push_back(11);
    s.push_back(22);
    s.push_back(33);
    s.push_back(55);
    for (int it=s.begin();it!=s.end();it++){
        cout<<*it<<endl;
    }
}

Line 12: error: cannot convert '__gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<int*, __gnu_norm::vector<int, std::allocator<int> > >, __gnu_debug_def::vector<int, std::allocator<int> > >' to 'int' in initialization

The auto keyword simply allows you to take advantage of this knowledge - if you (compiler) know the right type, just choose for me!

UncleBens
i am using visual c++ 2010 so my compiler has not any problem with auto keyword
Nice explanation. +1
jalf
I especially that like that `auto` is a *heluvalot* shorter than the actual type.
caspin