I have just installed gcc 4.4.1 and started to use std::initializer_list
in my code. Now I want to start use auto. I'm a bit reluctant to auto
since I can't really see the use for it except for making the code look
cleaner.
There is probably only one place where I will start use it. Example below.
std::vector<int> vec;
vec.push_back( 7 );
vec.push_back( 55 );
for( std::vector<int>::iterator i = vec.begin(); i != vec.end(); ++i )
std::cout << (*i) << std::endl;
... will be ...
for( auto i = vec.begin(); i != vec.end(); ++i )
I find for loops being a good place where to use auto since we know we
are dealing with iterators.
Now, before I start change all for loops, are there any recommendations
that you have for me, i.e. do I use auto the proper way?
Also, if you can convince me to use auto in other situations, that
information would also be welcome.
Best regards,
Daniel
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
and who would want a thing like that???
> There is probably only one place where I will start use it. Example below.
>
> std::vector<int> vec;
> vec.push_back( 7 );
> vec.push_back( 55 );
>
> for( std::vector<int>::iterator i = vec.begin(); i != vec.end(); ++i )
> std::cout << (*i) << std::endl;
>
> ... will be ...
>
> for( auto i = vec.begin(); i != vec.end(); ++i )
>
> I find for loops being a good place where to use auto since we know we
> are dealing with iterators.
We know we are dealing with iterators because of begin() not because
of the for loop.
What about
auto x = vec.find(42); // another iterator
or
auto y = aStdMap.insert( make_pair(1,2)); // you realy don't want to
write this one out
> Now, before I start change all for loops, are there any recommendations
> that you have for me, i.e. do I use auto the proper way?
Don't change anything.
Why would you try to make your code less portable?
Not all compilers support it yet.
If it doesn't seem clearer to you to use auto then there is no reason
at all to use it.
> Also, if you can convince me to use auto in other situations, that
> information would also be welcome.
I expect it to be used extensively in template functions rather than
using cumbersome typedefs such as
typename C::ATypeInTemplateParameterC myVar
and wherever we have functions that exist specifically to avoid naming
template types:
auto myPair = std::make_pair(a,b);
Also I will be using it specifically to try to avoid using knowledge
of a concrete type: Often you need a variable to model a certain
concept. Using any functionality it has beyond that concept therefore
makes the code less general. Whenever someone sees a familiar class
they automatically feel free to use any member of that class - That is
one of the advantages of OO. My thinking is that if I use auto then
just maybe future modifiers of my code are more likely to look at the
comments describing what it is supposed to do rather than just
considering what the class is capable of.
I've no experience to back this up as auto is so new so it could just
be wishful thinking.
The new auto keyword is similar to the "var" keyword which has been in
C# for a while now. Not everyone agrees it has made code more
readable, even when intellisense can show you the actual type of the
"var" variable. There's an interesting discussion here,
http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c.
When I've been programming in C#, I use var within for loops,
declaring variables of complex generics and when constructing
anonymous types via LINQ queries, elsewhere I prefer to specify the
datatype explicitly. Once more C++ compilers start supporting auto, at
the moment I can't see myself using auto outside of for loop
constructs, since I already make extensive use of typedefs (which I
don't find cumbersome) to try to make code more readable. I also use
nested typedefs in type traits classes when creating generic
abstractions, and I can't see how auto can replace typedef in such
cases .. but I'm open to other ideas.
Cheers