Powerful 'auto' type in C++11/C++14/C++17

Updated: Jul 16, 2018


In case you follow advent and evolution of C++ then you will know by now that C++17 is here! It already started getting into mainstream compilers. GCC added support for new standard with GCC5 and onwards. If curious check this out.


But that is not exactly the topic of discussion here, we want to look at something cool and unique C++ added with C++11 and onwards. It’s definitely not the cool but one of the cool features in C++11 and onwards now. It is – ‘auto’ type in C++.


So what is the auto type and what is all this fuss about it? It is new type introduced with C++11 and as the name suggests, when the variable is declared of type ‘auto’, its type is deduced dynamically than statically always typing it. Note that though the type is still determined compile time, with the advent of Template Metaprogramming (TMP) techniques, this is still super powerful and useful. You can view ‘auto’ types as ‘any’ type if you use boost or POCO like open source libraries in C++.


How do you declare auto vars? Here are some examples:



Each one of above auto var will be type deduced dynamically now. Comment on each one explains type deduced for each auto var.


One noticeable thing is const’ness and reference’ness is removed for types when auto is declared non-const, non-reference type. Template instantiation in C++11 onward uses similar rules so it is also sometimes called template type deductions.


Auto types also can’t be declared without defining them. That is a useful technique on its own as non-initialized variable declarations have costed unaccountable issues to unaccountable developers across the globe! But with auto type, you enforce compiler firewall in helping you make sure variables are always initialized with appropriate default.


Apart from auto type variables, the auto type can also be the return type of functions. For template definitions, that is quite a useful thing. Let's look at an example below:


Now if you know type checking system well for C++, you would realize that not needing to specify a return type for a function like Sum is indeed useful on many occasions.

The auto types can also be used in lambda functions in C++ now. One other use case for auto is writing sweet and short loops in C++11 (includes C++14 and C++17), e.g.


The auto-type also makes code explicit and self documenting. Let's look at one other example:


Now here, the result of DoSomething is auto, too. That is helpful to not hardcode return type. Same applies to ‘result’ var storing results. It can be of the type which is a specific pointer to “something”, but let's say tomorrow we change the return type of DoSomething, then that would mean you have to go back and change the type of ‘result’ like variables affected by this change. That could end up as a nightmare, especially if DoSomething is part of the common library. It would mean all client code using this library interface would need to change and recompile otherwise.


Similarly, if you were a writing the client code for the library, where DoSomething is defined and say it defines a specific return type, not auto, then using ‘auto’ type for the ‘result’ variable would mean you as a client of that library, shield yourself from such future changes in the library. If ever DoSomething changes its return type, only affected change for you as a client of that library would mean re-linking! That is enough of an advantage to using auto to store results of such functions.


Conclusion:


‘auto’ type introduced with C++11 is the very powerful type. C++ uses a specific set of rules while deducing type for variables declared as 'auto'. The auto-type can also be return type for functions and can also be used in lambda functions. Auto variables also help write beautiful and short loops on STL containers with newer for-loop syntax in C++11 (and onwards). Using auto-type for storing the result of common library functions help shield your client code from future library interface changes.