Sasha Goldshtein is a Senior Consultant for Sela Group, an Israeli company specializing in training, consulting and outsourcing to local and international customers.Sasha's work is divided across these three primary disciplines. He consults for clients on architecture, development, debugging and performance issues; he actively develops code using the latest bits of technology from Microsoft; and he conducts training classes on a variety of topics, from Windows Internals to .NET Performance. You can read more about Sasha's work and his latest ventures at his blog: http://blogs.microsoft.co.il/blogs/sasha. Sasha writes from Herzliya, Israel. Sasha is a DZone MVB and is not an employee of DZone and has posted 202 posts at DZone. You can read more from them at their website. View Full User Profile

SDP December 2011: Everything New in C++

12.09.2011
| 3637 views |
  • submit to reddit

Noam and I delivered on Tuesday a joint session called Everything New in C++ at the SELA Developer Practice. It’s been a really fun session to work on, even though it was also a cold reminder how easy it is to forget “The C++ Way” when you stay away for a little while. The new C++ standard is not just a set of minor additions to the C++ language and libraries—it almost feels like a whole new language, what with the lambda functions, type inference, and rich concurrency libraries.

image

While we were planning this full-day C++ session, Noam and I decided to focus not only on the standard C++, but also on some of the emerging extensions and runtimes around it. Because this day was the only day aimed directly at C++ developers, we wanted to cover as much of the new ground as possible. And indeed, we did a lot:

First, I talked about the new C++ standard. We couldn’t discuss in depth all the features, but obviously the first to mention were automatic type inference (a.k.a. auto), lambda functions, and rvalue references. Explaining the motivation for rvalue references—especially around perfect forwarding—is always tricky, and it’s been great to see an understanding audience of seasoned C++ developers applaud the reasoning behind the new language features.

int fib1 = 1, fib2 = 1;
auto next_step = [&]() {
  int temp = fib2; fib2 = fib2 + fib1; fib1 = temp;
};
for (int i = 0; i < 20; ++i) next_step();
cout << fib1 << " " << fib2 << endl;

int n = 10;
auto say_yes_n_times = [=]() mutable ->bool {
  return (--n > 0);
};

(Two examples of lambda functions, the biggest feature—in my opinion—in the C++11 standard.)

Next, I talked about the “new STL”—the additions to the C++ standard library that made it through the standard, with a specific focus on concurrency (std::thread and its kin). I also mentioned regex support and unordered containers, which should have been part of the STL a long time ago.

template <typename Future>
void wait_all(initializer_list<Future> l) {
  for each (const auto& f in l) { f.wait(); }
}

template <typename RAIter>
void quick_sort(RAIter begin, RAIter end) {
  RAIter p = partition(begin, end);
  auto left = std::async([=]() { quick_sort(begin, p); });
  auto right = std::async([=]() { quick_sort(p++, end); });
  wait_all({left,right});
}

(Naïve parallel QuickSort using lambda functions, the async library function, futures, and initializer lists.)

Next, Noam talked about ConcRT—the Microsoft-specific concurrency runtime for C++ applications which shifts concurrency from being about threads to being about tasks and data-parallel algorithms. Noam used a couple of great textbook examples taken from the Win32 Concurrent Programming course that demonstrate exactly how easy it is to break recursive algorithms into tasks and to parallelize data-oriented algorithms with the data-parallel APIs.

combinable<int> sum;
parallel_for_each(matrix.begin(), matrix.end(), [&](row& r)
{
  for each (int i in row) sum.local() += i;
});
int total_sum = sum.combine(
  [](int a, int b) { return a+b; });

(Using the parallel_for_each ConcRT algorithm to parallelize matrix summation with partial sums for each thread aggregated into a combinable<int>.)

Finally, we moved to unexplored-land, the emerging-but-not-implemented-and-completely-nonstandard extensions to C++. Noam talked about C++/CX (a.k.a. /Zw), the language extensions for working with WinRT in Windows 8. These neat language extensions (very similar to C++/CLI) make consuming and exporting WinRT components very easy despite their COM-laden nature.

I had very little time left to talk about C++ AMP, a set of APIs and minor language extensions which make standard C++ code run on the GPU with as little extra work as a lambda function:

void MatrixMultiplyAMP(
  vector<float>& vC, const vector<float>& vA,
  const vector<float>& vB, int M, int N, int W) {

  array_view<const float,2> a(M,W,vA), b(W,N,vB);
  array_view<writeonly<float>,2> c(M,N,vC);
  parallel_for_each(c.grid,
    [=](index<2> idx) restrict(direct3d) {
      int row = idx[0]; int col = idx[1]; float sum = 0.0f;
      for(int i = 0; i < W; i++)
        sum += a(row, i) * b(i, col);
      c[idx] = sum;
  });
}

(Matrix multiplication using C++ AMP, non-tiled, adapted from Daniel Moth’s blog post.)

Thanks for attending this session, and we truly hope that you like the direction C++ is taking. It’s really the renaissance of C++ in the 2010s, starting with Visual Studio 2010 and the new C++ standard, and going forward with Windows 8 support, general purpose GPU programming, and game development. You can download some of the code demos and exercises we used during the day from here.

I have been recently posting short updates and links on Twitter as well as on this blog. You can follow me: @goldshtn
References
Published at DZone with permission of Sasha Goldshtein, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Afandi Merathi replied on Sat, 2012/03/24 - 8:15am

Cool!

when this version relaesed?

which Compilers support in this c++ version ?

thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.