DCSIMG
SDP December 2011: Everything New in C++ - All Your Base Are Belong To Us

All Your Base Are Belong To Us

Mostly .NET internals and other kinds of gory details

SDP December 2011: Everything New in C++

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

Comments

Reader said:

Cool!

when this version relaesed?

which Compilers support in this c++ version ?

thanks

# December 9, 2011 3:55 PM

M said:

Hi! Any chance for the slides and/or video of the talk? :-)

# December 10, 2011 12:34 AM

Sasha Goldshtein said:

@Reader: This link has details on compiler support: wiki.apache.org/.../C++0xCompilerSupport

@M: Yes, but not right away. We will publish them on the SELA College Channel (http://scc.sela.co.il) in a few weeks.

# December 13, 2011 1:18 PM

grumpy said:

I'm fine with the direction C++ is taking. Less so with the (lack of) direction Visual C++ is taking.

If VC11 actually supported some new C++11 features, it might be possible to take it seriously. At the current rate (looking at the changes from VC10 to VC11) we're looking at 2050 *at the earliest* before the major parts of C++11 are supported in Visual Studio.

On the C++/CX front, I'm less than ecstatic too. Sure, I see the point in making it, and if it was just there as an interop layer between C++ and WinRT, it'd be great.

But instead, we get a new language which adds several features whose primary purpose seem to be to break compatibility with C++ and encourage sloppy code (partial classes), and even though some very minor changes could have made it possible to aid developers in separating C++ and C++/CX layers (the infamous "you should use C++/CX at the boundary only" advice which, hilariously, Microsoft admits they haven't even *tried* doing themselves), that was not even considered. And because the CX extensions are much broader in scope than is necessary to achieve its stated goal, there are a huge set of interactions between C++ and C++/CX features which, according to comments made in discussions on the VCBlog and C9, not even the VC++ team fully understands.

And obviously, feedback on this point is silenced with the familiar line that "you raise a good point, but it's too late to change the design now".

Great.

So yeah, I'm happy about the direction C++ is taking, but the direction of VC++ is going straight backwards, and shows no sign of changing.

I remember a couple of years ago, when, for a brief period, it actually seemed like the VC++ team had at least a desire to stay in touch with the real world.

# January 10, 2012 10:47 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: