Visual Studio 11: The Early Release
Despite some harsh words about Visual Studio 11's UI, I'm finding that it makes my heart go pitter-pat every time I use it. Why? Because this early release is finally incorporating a decent set of long-awaited C++11 features. In this article, I'll show you how a little thing like a lambda can make a big difference in your coding style.
Microsoft and C++ — We Have History
Microsoft has a cyclic relationship with C++. In the early MFC days, the love was there big time — you had access to most of the system API using C++. However, around the turn of the millennium, Microsoft came under the Rasputin-like influence of Anders Hejlsberg and his beloved offspring, C#. Now it appears that maybe the pendulum is swinging back a bit, and C++ is no longer viewed as an afterthought. Great news.
Although Visual Studio 11 is a developer's preview, Microsoft is saying that it is production ready — you can use this to create programs that are ready for release. In addition to touting a complete implementation of the C++11 standard library, an impressive list of language features have been turned on as well. (N.B. the path ahead is still long and arduous.)
Modern C++
Before even using Visual Studio C++ 11 to test a single line of code, I really appreciated reading Welcome Back to C++ (Modern C++), a manifesto that includes the following bullet points:
Modern C++ emphasizes:
std::string and std::wstring types instead of raw char[] arrays.vector, list, and map — instead of raw arrays or custom containers.
I believe all of these changes result in safer code that is easier to read and maintain, without giving up the type-safety and efficiency that we love so much. Fully implementing these features either leans heavily on C++11 or requires it outright.
A Simple Example Using Naive C++98
It's interesting to watch the evolution of code from C++98 to C++11 and see how it affects your code. You'll find that the transformation can make it look like you are literally using a new programming language.
In this simple program, I'm taking a Scrabble rack of tiles and whipping through the Scrabble dictionary to find matches. Since it is a one-time call, I'm not storing the words, just doing a quick online comparison. In C++98, my code might have looked like this:
void find_matches( std::string rack, const std::string &filename )
{
std::sort( rack.begin(), rack.end() );
std::ifstream sowpods( filename.c_str() );
std::string word;
while ( sowpods >> word ) {
std::string sorted = word;
std::sort( sorted.begin(), sorted.end() );
if ( sorted == rack )
std::cout << word << " ";
}
}
int main(int argc, char* argv[])
{
find_matches( "etaionsr", "sowpods.txt" );
return 0;
}
This works properly and I get what looks like correct output:
anoestri arsonite notaries notarise rosinate senorita
Classes Good, Templates Better
As people started to get more comfortable with templates and iterators, algorithms like this were commonly rewritten to take a range of iterators as input — much as the standard library algorithm functions do. This meant changing the function to a template function, but it did make it a lot more flexible. I could now call the function to operate on data from a file, just as before, but I can also now use any other container, or even an array, as input:
template<typename ITERATOR>
void find_matches( std::string rack, ITERATOR ii, ITERATOR jj )
{
std::sort( rack.begin(), rack.end() );
for ( ; ii != jj ; ii++ ) {
std::string sorted = *ii;
std::sort( sorted.begin(), sorted.end() );
if ( sorted == rack )
std::cout << *ii << " ";
}
}
int main(int argc, char* argv[])
{
std::ifstream sowpods( "sowpods.txt" );
find_matches( "etaionsr",
std::istream_iterator<std::string>( sowpods ),
std::istream_iterator<std::string>() );
return 0;
}
More or less the same number of lines of code, but it is now generic.

