Auto Type Determination in C++0x

The C++0x standard defines a feature called auto type determination. It is similar to the var keyword in C#. It allows you to let the compiler figure out the type of a variable. For example:

auto something = 123;

The compiler will automatically figure out that something is of type int. The above is a very simple example and in this case the developer can easily figure it out. However, with template types and template meta programming techniques it can become difficult for the developer to figure out what the type exactly is.
Consider the following loop:

for (vector<string>::const_reverse_iterator criter = vec.rbegin(); criter != vec.rend(); ++criter) {...}

With the auto feature this could be rewritten as follows:

for (auto criter = vec.rbegin(); criter != vec.rend(); ++criter) {...}

which is much easier to type.

This auto feature is also useful for variables that store a function pointer or lambda expressions because the compiler will figure out the exact type for us. Lambda expressions will be briefly described in another blog post.

Share

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: