Introduction to C++ 11 Series – Part 6, auto – an old keyword with new meaning

March 27, 2013

Originally, auto declares a variable in the automatic storage, which is also the default behavior. This is still the behavior of the C language. In C++ 11, auto declares a variable, whose type is deduced from the initialization expression. If you are familiar with the C# var keyword, auto is almost the same. The main difference is that modifiers such as const, &, *, **, &&, static, and volatile can be added to the auto to enhance type declaration.

auto is a productivity feature, it lets you write code faster. auto is also an abstraction enhancer; you can change expression types without changing the code as long as the type meets the code constraints. auto is great when using templates, STL, Iterators, Lambda functions, everything…

int answer = 42;

auto &question = answer;

auto *p_question = &answer;

auto d1(make_shared<Derived1>(Derived1()));

auto d2 = make_shared<Derived2>(Derived2());

auto ptr_base = answer > rand() ? shared_ptr<Base>(d1) : shared_ptr<Base>(d2);

auto i(answer * 3);

auto x = i, *p = &i;

const auto y = x;

auto *z = new auto(i + 20);

auto func = [](int a, int b){ return a + b; };
list<stack<map<vector<char> ,long long>>> could_use_a_typedef
;
auto could_use_a_typedef_iterator = begin(could_use_a_typedef
);

 

To explore auto’s resulting type, I wrote an example that prints out the variable’s type information. I am using a macro so that the local variable does not change type when the variable is passed to a function as an argument:

#include "stdafx.h"

#include <iostream>

#include <type_traits>

#include <string>

#include <functional>

using namespace std;

 

void show_types();

 

#define TypeidOf(t, msg) \

       cout << msg << ‘:’ << endl; \

       cout << "Typeid: " << typeid(t).name() << endl; \

       if (is_volatile<decltype(t)>::value) cout << "volatile" << endl; \

       if (is_const<decltype(t)>::value) cout << "const" << endl; \

       if (is_pointer<decltype(t)>::value) cout << "pointer" << endl; \

       if (is_reference<decltype(t)>::value) cout << "reference" << endl; \

       cout << endl << endl;

 

 

int _tmain(int argc, _TCHAR* argv[])

{

       auto x = 1;

       TypeidOf(x, "auto x");

 

       auto &rx = x;

       TypeidOf(rx, "auto &rx");

 

       auto *px = &x;

       TypeidOf(px, "auto &px");

 

       auto **ppx = &px;

       TypeidOf(ppx, "auto **ppx");

 

       auto **also_ppx = &px;

       TypeidOf(also_ppx, "auto **also_ppx");

 

       static auto sa = 8;

       TypeidOf(sa, "static auto sa");

 

       volatile auto va = sa;

       TypeidOf(va, "volatile auto va");

 

       volatile auto &rva = sa;

       TypeidOf(rva, "volatile auto &rva");

 

       auto const d = 1.23;

       TypeidOf(d, "auto const d");

 

       static const volatile auto * volatile const pd = &d;

       TypeidOf(pd, "static const volatile auto * volatile const pd");

 

       show_types();

 

       return 0;

}

The result is:

clip_image001

 

As I mentioned before, to use the exact type I had to use a macro. I also had to use the new decltype operator to get the type from the variable in order to use type_traits. I will write about decltype and about type_traits in a future post. To show that we can have the same results using a template, I added another function – show_types(). This time I am using r-value references and a perfect forwarding technique to pass the exact type to the typeid_of function. I will write about r-values and perfect forwarding in later posts.

Here is the code:

 

template <class T> void typeid_of(T &&t, const string &msg)

{

       cout << msg << ‘:’ << endl;

       cout << "Typeid: " << typeid(t).name() << endl;

       if (is_volatile<T>::value) cout << "volatile" << endl;

       if (is_const<T>::value) cout << "const" << endl;

       if (is_pointer<T>::value) cout << "pointer" << endl;

       if (is_reference<T>::value) cout << "reference" << endl;

       cout << endl << endl;

}

 

void show_types()

{

       auto x = 1;

       typeid_of(forward<decltype(x)>(x), "auto x");

 

       auto &rx = x;

       typeid_of(forward<decltype(rx)>(rx), "auto &rx");

 

       auto *px = &x;

       typeid_of(forward<decltype(px)>(px), "auto &px");

 

       auto **ppx = &px;

       typeid_of(forward<decltype(ppx)>(ppx), "auto **ppx");

 

       auto **also_ppx = &px;

       typeid_of(forward<decltype(also_ppx)>(also_ppx), "auto **also_ppx");

 

       static auto sa = 8;

       typeid_of(forward<decltype(sa)>(sa), "static auto sa");

 

       volatile auto va = sa;

       typeid_of(forward<decltype(va)>(va), "volatile auto va");

 

       volatile auto &rva = sa;

       typeid_of(forward<decltype(rva)>(rva), "volatile auto &rva");

 

       auto const d = 1.23;

       typeid_of(forward<decltype(d)>(d), "auto const d");

 

       static const volatile auto * volatile const pd = &d;

       typeid_of(forward<decltype(pd)>(pd),
                    
"static const volatile auto * volatile const pd");

}

 

The result is identical to the previous macro based sample:

clip_image001[5]

You can read more about auto here.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

3 comments

  1. nipreineeMay 9, 2013 ב 14:36

    Hello! I just would like to give a huge thumbs up for the very good info you have here on this post. I will be coming back to your weblog for extra soon.

    [url=http://cheapchinajordansa.noads.biz/]cheap jordans[/url]

    Reply
  2. windows 7 home premium keyJuly 22, 2013 ב 07:28

    Each of the Heddon fishing lures have been created in order to offer an ever more rewarding expertise to fishermen.

    Reply
  3. 779September 4, 2013 ב 23:54

    I can’t knit – i want to learn – but i love everything in that photo. where it is from, mystic? that yummu raw edge tan leather oversize bag. 1970s singer with a divine voice. and i know those big fat needles will make those big fat knits that i go a little bit crazy for…

    Reply