Concepts are out of C++0x

Concepts were supposed to be an important new feature in C++0x. They were meant to allow programmers to specify properties (like constraints) for templates, allow compilers to do some optimization and tools to do some formal checking on the code. After years of debate, the standard committee found them “untried, risky and controversial” and ruled them out last month during their meeting in Frankfurt.

Danny Kalev, a former member of the C++ standard committee, wrote about this controversial removal, and later interviewed Bjarne Stroustrup about the concepts and the future of C++. You can read this interview, published on DevX.com, here.

You can find more about concepts in this paper by Bjarne Stroustrup.

, , , Hits for this post: 29008 .

The new C++ standard defines a new keyword, static_assert, that is already available in Visual Studio 2010 CTP. This new feature allows introducing compile time asserts. It takes an expression that can evaluate to bool and a string. If the expression evaluates to false, the compiler issues an error with the given string literal. If the expression evaluates to true, static_assert has no effect.

Here is an example for using static_assert. Suppose you want to create a template vector class, but you don’t want to allow vectors with a size smaller than 4. Then you can use a static assertion to enforce that.

Compiling this program triggers an error at the second declaration.

Most of the use cases for this feature, in my opinion, would be to test on the size of different types. For instance this sample is taken from the C++0x draft.

To me, static_assert looks like a niche feature. It would have been great if this could be used together with some other features to enfore compile time constraints on types. For instance, I want to restrict the possible types for a template class or function to only those that are derived from IListener (a fictive class).

Perhaps a future standard version will offer support for such things.

, , , , , Hits for this post: 50573 .

Lambdas in C++

The new C++0x standard adds lambda expressions to the language. Visual Studio 2010 CTP is already supporting this new feature that brings functional techniques to C++ too.

What is a lambda expression? It’s basically a function. In F# it’s an anonymous function, in C# it’s an anonymous delegate; in C++ it’s in fact an anonymous function object. Whenever you create a new lambda function, the compiler creates a function object for you.

That is equivalent to:

Of course, the lambda functor can be more complicated, when the lambda function captures state from the local scope. However, that is beyond the scope of my post. I recommend that you read more about lambdas in C++ on the VC++ blog.

Question is, what are these lambdas good for? Well, they mostly come in handly with algorithms that take predicates (function objects) as arguments. I’ll try to give you some examples in this post.

Let’s first consider a filter function, that takes a sequence (vector of T) and a predicate that indicates what values should be filtered, and returns a new sequence. That would look like this:

We can use this filter function to extract the odds numbers from a sequence (vector).

You could see in the above example that a second lambda function is used for printing the numbers to the console.

Since the Filter function is a template function it could be used with other types too. In the next example we’ll see how to filter words that have at least 4 letters from a sequence of words.

Let’s consider a second example, a Find (template) function, that takes a sequence and a predicate (that checks a condition for an element), and returns the first element in the sequence for which the predicate returned true.

We’ll use this function to find the first element in a sequence that is greater than a given value.

If you input 4 for instance, it will return 5. If you input 10, an exception will be thrown. You can see that this time the lambda function is [min](int i){return i > min;}. This means that it captures by value the min variable from the local scope, so that it can compare each element with that given value.

The last example I’m going to show is an accumulator function (also known as aggregate or fold). This function takes a sequence of elements, a seed (or initial value) and a function that specifies how to aggregate the elements, and returns the aggregate.

First, we can use it to compute the sum of all elements in a sequence.

The first lambda function above sums the current element with the previous sum, which initially is given as 0. The result is 55. The second lambda function multiplies the current element with the previous product, which initially is 1. The result is 3628800.

But the Aggregate function can be used with other types too. Here is a last example with strings.

These were several examples of how lambda functions help us write more generic, and less verbose code. I suggest you read more about lambdas in C++ here.

, , , , , , , , Hits for this post: 48284 .

The new C++0x standard provides support for type inference. The auto keyword that was doing nothing in C++ was given a new meaning: a placeholder for a type inferred by the compiler. For those familiar with C#’s var keyword, this is basically the same.

Here is a comparison between auto in C++ and var in C#.

C++ C#

with the output

C++ C#

As you can see, the use is very similar, except that in C++ you can use auto for a lambda expression, while in C# you cannot. The C# compiler raises an error, CS0815, when you try to use var with an anonymous function expressions, a lambda expression, a method group expressions, or the null literal expression, because these don’t have a type.

Just like with var, you can only use auto locally, and not as return type from a function, or parameter to a function.

While it might not be of that much help for ints or strings, using auto for instead of vector< int >::iterator, or map< string, list< string >>::const_iterator comes in handy. The auto type inference helps us write less verbose code.

In addition to auto, the C++0x standard introduces a new keyword, called decltype (think of it as ‘typeof’), that can be used to determine the type of an expression at compile time.

However, this new keyword was not yet introduced in Visual Studio 2010 CTP. According to Stephan T. Lavavej from the VC++ team, it might be possible to be introduced in a next CTP or beta version.

Implementing auto doesn’t implement decltype (C++0x’s name for what was previously called typeof) for free.

Paraphrasing our libraries and compiler front-end program manager Damien Watkins, the CTP is only the first look at our VC10 functionality and there is definitely more to come. We understand that certain features “go together”, and that adding complementary features is a good thing in general. As always, customer feedback is a vital resource in forming our plans.

, , , , , , , , Hits for this post: 63257 .