C++11: ‘auto’

The new standard of C++ is being released and it exposes a lot of features. Starting this post, I will write about such new features, how they are used and their benefits.

Some bits of history: The last published C++ standard is called C++03 and was published, oh surprise!, in 2003. Since such day, a lot of compilers implemented the features described by that standard. The standard committee continued to work on the next standard and they codenamed it C++0x. The ‘x’ meant it should have been published between 2003 and 2009 but it was not released in such dates; so, the people started to joke saying the ‘x’ means TEN in roman notation or the same Bjarne Stroustrup joked on it saying the ‘x’ stands for any hexadecimal value ;)

This new standard contains a lot of improvements to the core language as well as a lot of additions to the standard library.

The aims for this new version, according to Bjarne Stroustrup are making C++ a better language for systems programming and library building, and making it easier to learn and teach.

auto is an already existing keyword inherited from C that was used to mark a variable as automatic (a variable that is automatically allocated and deallocated when it runs out of scope). All variables in C and C++ were auto by default, so this keyword was rarely used explicitly.

In C++0x, auto is used to infer the type of the variable that is being declared and initialized. For example:

int a = 8;
double pi = 3.141592654;
float x = 2.14f;

in C++0x can be declared in this way:

auto a = 8;
auto pi = 3.141592654;
auto x = 2.14f;

In the last example, the compiler will infer that a is an integer, pi is a double and x is a float.

You can say: “come on, I do not see any advantage on this because it is clearly obvious that ‘x’ is a float and it is easy to me infer the type instead of letting the compiler to do that”, and yes, though you will always be able to infer the type, that is not always as evident or easy as thought. For example, if you want to iterate in a STL vector in a template function using C++0x, the code would be something like:

template <typename T>
void show(const vector<T>& vec)
{
    for (auto i = vec.begin(); i != vec.end(); i++) //notice the 'auto' here
        cout << *i << endl;
}

The

auto i = vec.begin();

in ‘old’ C++ would have to be written as:

typename vector<T>::const_iterator i = vec.begin();

So, in this case, the auto keyword is very useful.

Anyway, there are restrictions on its usage:

  • You cannot use auto as a type of an argument of a function or method.
  • Your method return type cannot be auto
  • You cannot use auto to declare and initialize variables of several types in the same line, like this:
    auto x = 2, b = true, c = "hello"; //invalid auto usage
    
Advertisements

2 thoughts on “C++11: ‘auto’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s