Channels ▼

Mark Nelson

Dr. Dobb's Bloggers

Visual Studio 11: The Early Release

March 15, 2012

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:

  • Stack-based scope instead of heap or static global scope.
  • Auto type inference instead of explicit type names.
  • Smart pointers instead of raw pointers.
  • std::string and std::wstring types instead of raw char[] arrays.
  • Standard template library (STL) containers — for example, vector, list, and map — instead of raw arrays or custom containers.
  • STL algorithms instead of manually coded ones.
  • Exceptions, to report and handle error conditions.
  • Inline lambda functions instead of small functions implemented separately.

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.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video