Advertisement Jump to content
Sign in to follow this  

visual C++ 2010 auto keyword help

This topic is 2591 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

According to micrsoft visual studio 2010 uses the auto keyword like so
the auto keyword declares a variable whose type is deduced from the initialization expression in its declaration

can anyone please explain with a simple example of how/what the purpose of this is exactly? im confused on what microsoft is trying to say :(

Share this post

Link to post
Share on other sites
It can be useful to have the compiler deduce the type automatically

auto i = 5; //equivalent to int i = 5;

it's more useful to avoid lots of extra typing:

instead of:

for( std::vector<int>::iterator iter = vec.begin(); ...

you can say:

for( auto iter = vec.begin(); ...

It's even more useful (or necessary) to use when you can't spell the type of a variable, in the case of lambda functions:

auto f = [](int n) { std::cout << n; };

Share this post

Link to post
Share on other sites
It is useful for types that are awkward or difficult for the programmer to come up with, but are easy for the compiler to figure out.

Many times when you combine namespaces and templates the code is very explicit but unnecessarily awkward.

Consider a vector class. It contains strings. You want to iterate over the continaner. Your loop could end up looking like this:

for(std::vector<std::string>::iterator it = filenames.begin(); it!= filenames.end(); ++it) ...

The compiler can already figure out what kind of iterator is given with myarray.begin(), so it can now be written as:

for( auto it = filenames.begin(); it != filenames.end(); ++it) ...

Generally it is more clear and easier to maintain with a typedef:

typedef std::vector<std::string> FileNameCollection;

for( FileNameCollection::iterator it = filenames.begin() ...

With that, you are allowed to just use FileNameCollection everywhere and if you later change from a std::vector to another type, like std::deque, you only change it in a single place.

The only places I know of where you cannot use a typedef like that is lambda functions (that can be referenced as function pointers) or if the type contains a template type. Fortuntely the first can be handled easily, and the second is a very rare situtation. In those situations the auto keyword can save programmers from a rather painful workaround where they must deduce something the compiler knows.

Personally I prefer programmers not use them except in those two cases, favoring a typedef that any programmer can easily see and debug, rather than a simple 'auto' that causes the programmer to dig in deep to figure out the class that the compiler determined is the best match; it is already a source of bugs where programmers don't understand the compiler's rules and assume the auto deduced type is one thing, but is actually another.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!