I am becoming more and more excited about C++0x these days. It would be interesting to see some elegant examples of the new power. What is your favorite sample that makes other C++ programmers smile?

For me it was the following one which I saw in Wikipedia, I just modified it little bit:

uniform_int_distribution<int> distribution(1, 6);
mt19937 engine;
auto dice = bind(distribution, engine);
int random = dice();  // Generate a uniform integral variate between 1 and 6

The beautiful things about this code, is that it shows the new random number stuff in the enhanced std library. The use of functional composition with auto-inference is really powerful and eye pleasing. I am amazed how someone could write code at this level of abstraction, and in the same time the compiler has all the information to optimize this code greatly.


Initializer list:

std::vector<int> vec = { 1, 2, 3, 4, 5, 6, 7 };


// (removes odd numbers), I'll never write a functor again.
vec.erase(std::remove_if(vec.begin(), vec.end(), [](int x) { return x % 2 == 1; }), vec.end());

Range-based for loop:

// auto in loops is good, but this is even better
for (int i : vec)
    std::cout << i << std::endl;

I won't mention anything from TR1 since we should have had this long time ago.


By far the biggest deal is lambdas, since they let us write functions that generalize over code, while making them elegant to use. Of course, functors were there before, but defining one at point of call was tedious, especially when you had to capture part of its local environment. With lambdas, it's not an issue anymore:

std::vector<person> people;
person& p = ...

std::find_if(people.begin(), people.end(),
    [&](const Person& x) { return x.name == p.name });

The codebase I work on is built using VC++2010, and we use the new features rather heavily in new parts of code. Of them all, I see lambdas used by far most often, both to conveniently use STL algorithms, and to refactor code to extract common logic into function templates that take lambdas (as functors) as parameters.


My STL container for-loops reduces to:

std::vector<int> myints;
for (int & x: myints) {
  x *= 2;



Yes, I know it is legal in C++98, but with C++0x move semantics it is actually efficient enough to be used in production code :)