C++ is a statically typed language by design. In other words, you have to specify which data type your dear variable is supposed to be. If you declare myCoolVar to be an integer, you can’t let him play with his string friends. Type checking is done at the compile time. So myCoolVar will soon be caught red-handed if it goes matchmaking with a string or a vector.
This is contrast to the radical kids in the block like Python and Ruby who don’t care whom their variables play with. They call themselves dynamically-typed. A washed up floating point might become a variable with a character in a few moments.
Cool as it may sound, dynamic typing is not for everyone. C++ is mostly used where performance and reliability are key factors. Thus the C++ standard has always insisted on strong, static typing. But even if you haven’t been programming since the dinosaurs roamed the earth, you may know that it’s not always easy to remember and explicitly declare the correct variable type. The new C++ standard, or C++11 as it’s known, aims to change this.
C++11 isn’t exactly new to the city. Most compilers around, including gcc and Microsoft C++ compilers, now include support for C++11. Among the dozens of improvements it has brought, let’s have a look at the auto keyword.
Suppose you want to take the length of a C++ string called myStr. Easy-peasy. myStr.length() is the way to go. But wait. The return value of the length() function is string::size_t. Besides from being hard to type in, it’s not easy to remember all these return types either. That’s when the auto keyword comes handy. Instead of,
[code language=”cpp”]string::size_t len = myStr.length();[/code]
you can simply write
[code language=”cpp”]auto len = myStr.length();[/code]
The compiler detects the return type of myStr.length() and makes len a variable of that type. So very convenient.
Not convinced? Suppose you have a map of objects that you need to iterate. How do you initialize the iterator?
[code language=”cpp”]std::map<char,int>::iterator it = myMap.begin();[/code]
Ugh. That isn’t the nicest code snippet you’ve written. But hey, we have our friend auto:
[code language=”cpp”]auto it = myMap.begin();[/code]
Skadoosh! Auto has automagically made it an std::map<char,int>::iterator, because that’s the return type of myMap.begin()! Now that does look nice.
What if I want to declare a variable with a default value?
Suppose you want to declare a variable that holds the length of a string. But you first need to set it to zero.
[code language=”cpp”]auto len = 0;[/code]
Ha! That is wrong! This would make len an int, not string::size_t. But C++11 has an answer for that as well. You can use the decltype keyword like this:
[code language=”cpp”]decltype(myStr.size()) len = 0;[/code]
This declares len as a variable of type returned by myStr.size(), but still initializes it to zero.
Is this dynamic typing?
No, not really. Even though it appears as if auto changes a variable’s type dynamically, it actually happens during the compile time. It’s just that you don’t have to explicitly code the type of the variable, the compiler does that for yourself. Which is mighty sweet of C++11.
Is it safe to use auto?
Isn’t letting your variables roam free considered as bad parenting? No. Not only is it safe to use auto in your C++ code, it’s even recommended that you use it wherever possible. Bjarne Stroustrup, the creator of the C++ language himself, advocates its use. Just make sure that your compiler is updated to support C++11.
Image Credits : http://geekandpoke.typepad.com/geekandpoke/2010/01/best-of-both-worlds.html