Partners: KDAB Whole Tomato Software CppDepend

28 July 2012

Testing new features of C++11

C++ 11 has arrived and it would be nice to follow new standards and techniques that the new version of language provides. It is said that C++11 is an exciting new world so let try something out...
I tried to code some simple test applications that use new standard and also I refreshed knowledge from STL and Boost libraries.
Note that I did not explore all the things... it is just the beginning :)

Compiler Support

  • VC++  link to msdn - Unfortunately even VC11 (new VS 2012) does not support fully the new standard. But even in VS 2010 you can play with decent set of new features.
  • G++:  link to gcc - Generally support for C++11 started with the GCC 4.3, now we have version 4.7 and most of new features are supported. One need to add "-std=c++11" to instruct the compiler to use the new standard.
As wee can see we are probably in the middle (or a bit after the middle) of the way of introducing C++11 to compliers. On the other hand we are in the beginning of introducing C++11 to programmers! At least several years need to pass to make C++11 true standard. Just at it was when shifting from C++98 to C++03

Update: See C++ in 2013 and C++ in 2014 posts to see more about compiler support

Iteration over a sequence

Previously we could use:
vector<int> vec;
for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++i)
    doSomething(*it);
But now we can simply use:
for (auto v : vec)
    doSomething(v);
Isn't that great? With the 'auto' keyword and upgraded 'for' loop we can easily iterate over many types of sequences and don't bother with the type of an iterator.

Smart pointers

I've read in many places that raw C++ pointers should be abandoned as soon as possible and one should use smart_pointers whenever possible. I agree with that, but sometimes it is tempting to manage memory on your own and involve simple "*", not some strange looking pointers names...
C++11 offers:
  • std::unique_ptr - true unique pointer type that should be used as a replacement for the deprecated std::auto_ptr. As the name suggests this pointer cannot be shared - there is a strict ownership. If you have one pointer and you want somehow to transfer it to some other place you can use "move".
  • std::shared_ptr - reference counted smart pointer. This type of pointers are open to problems such circular references.
  • std::weak_ptr - 'helper' for shared pointers. This does not involve increasing reference counter for the pointers but before using it one needs to check if it still exists.

Hash tables

As we all know hash tables are one of the most important data structure used in programming. If you have some task to do just try hash table (other way is to use a data structure for graphs). So most of our problems can be solved by using hash maps or graphs :)
In C+11 we get true hash maps. Notice that std::map was usually implemented as balanced tree (AVL for instance). The std::hash_map was not part of the Standard library... and thus might not be portable among different compilers.
In the new C++ standard we get std::unordered_map which is an unordered associative container.
Some code for this feature:
vector<std::string> stringKeys(NUM_KEYS);
unordered_map<string, int> umap;
for (auto k : stringKeys)
{
    map[k] = 0;
}
The most notable difference between the std::map and std::unordered_map is a change in the template parameter list. In std::map we need to provide class Compare = less<Key> but in std::unordered_map we need to pass class Pred = equal_to<Key>

Lambdas

Lambda expression can be used as a functor object replacement. For instance:
vector<int> vec(10);
for (int &i : vec)
{
    i = rand()%100;
}
sort(vec.begin(), vec.end(), [](const int& a, const int& b) -> bool
{
    return a > b;
});

Conclusion

C++11 offers a lot of new features. It is important to learn them and use on a daily basis - not as a syntactic sugar that can be used from time to time. This language got quite complicated though (if we count not only the syntax but also STL/Boost libraries) but It gives a lot of power to programmers.
I hope to use this in my projects that are a bit larger than simple tests :)

Some links:

Books related to this topic:

Get my free ebook about C++17!

More than 50 pages about the new Language Standard.

C++17 in detail, by Bartlomiej Filipek

© 2017, Bartlomiej Filipek, Blogger platform
Any opinions expressed herein are in no way representative of those of my employers.
This site contains ads or referral links, which provide me with a commission. Thank you for your understanding.