Home > Articles > Programming > C/C++

C++ Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

C++0x Support in Visual Studio 2010

Last updated Jan 1, 2003.

Microsoft's Visual Studio is the most popular C++ IDE among Windows C++ developers. Such popularity enables the C++ community to get valuable feedback from independent developers about new features. In the following sections I will outline the major new C++0x features that VS 2010 currently supports.

Core and Library features

The Visual C++ compiler that's embedded in the VS development suit supports six new C++0x features: lambda expressions, auto, decltype rvalue references, static_assert, and nullptr. Additionally, the Standard Library includes several C++0x enhancements such as new algorithms and better support for move semantics. With respect to performance, Microsoft reports that the Visual C++ compiler generates more efficient code and that the linker has also been improved in several aspects. However, I will focus here mostly on the new C++0x features.

Lambda Expressions

Microsoft is among the first vendors to support this feature. Truth be told, I have mixed emotions about lambda expressions. They are simpler to code than handwriting full-blown function objects. However, C++0x lambdas aren't polymorphic and I suspect that they will be over-used (and even misused) because programmers will consider them nifty and trendy. The syntax isn't Elysian bliss either. However, we'll probably have to live with it because lambdas are in the FCD. It seems rather unlikely this feature will undergo major revisions now.

auto and decltype

auto and decltype are probably everyone's favorites C++0x core feature. auto lets you declare objects without stating their types explicitly. The compiler infers the type of the object from its initializer:

auto p = new int; //p is int*

Similarly, decltype returns the type of its argument (an expression, a literal or an object). You can use decltype to automate the return type of a function template, create typedefs and so on:

typedef decltype (5) XTYPE; //XTYPE is synonymous for int, the type of 5
XTYPE x=0;

Rvalue references

Rvalue references were added to C++0x to facilitate move semantics and perfect forwarding. In the original proposal, rvalue references could bind to lvalues. This however led to ambiguities of overloaded functions, and undesirable implicit conversions. Later, the rules were changed (this is known as "rvalues 2.0"). In the current FCD, rvalues cannot bind to an lvalue. VS 2010 implements rvalues 2.0, which allows you to overload a function based on the type of the reference:

void readval(int&& n);
void readval(int& n);
int main() 
{
 int x;
 readval(5); //#1
 readval(x);//#2
}

static_assert

You've heard of the #error preprocessor directive, which validates its argument during the preprocessing stage. You surely have seen and used the assert() macro, which is evaluated at runtime. The missing link was a compile-time assertion facility, though. The new keyword static_assert that was recently added to C++0x lets you evaluate a certain condition at compile-time. This feature is mostly useful for validation template arguments.

nullptr

The new keyword nullptr is used instead of the archaic and bug-prone NULL macro. nullptr represents a generic null pointer value. You can use it to initialize, assign or compare any pointer type, including pointers to members, pointers to functions and of course, pointers to data types:

int * n= nullptr;
if (n==nullptr)
 {//..
 }
int (A::*pmf)(char) =&A::f;
pmf=nullptr;

Standard Library Enhancements

A significant portion of the Visual Studio 2010 Standard Library has been rewritten to support C++0x features, most notable rvalue references. Standard containers such as vector and list now include move constructors and move assignment operators. Thus, vector reallocations now take advantage of move semantics by picking up move constructors of the vector's element type. Similarly, Standard Library algorithms recognize types that have move constructors and move assignment operators.

The new Standard Library also provides a smart pointer called unique_ptr which superseded the deprecated auto_ptr. Unlike the latter, unique_ptr safely implements move semantics so you can use it with STL containers and algorithms.

Finally, various new algorithms were added to the C++0x Standard Library including is_sorted() and iota() (one of my favorite algorithms!). The Visual Studio 2010 Standard Library implements these new algorithms. Here's an example that uses iota() to populate an array of 10 integers with the values 5,6...14:

#include <numeric>
int main()
{
 int x[10];
 std::iota (x, x+10, 5);//begin, end, initial value
}