The State of the Language: An Interview with Bjarne Stroustrup : Page 2

C++ founding father assesses the language on the eve of its new standard.

by Danny Kalev

Aug 15, 2008

Page 2 of 6

Concepts are probably the most important addition to C++ in years. Can you please explain which problems in contemporary C++ concepts will solve?

I suspect that what is most important depends on what you consider important. For many, concepts will be invisible. They will simply be "the magic" behind a radical improvement of the error messages you get when you make a mistake using something that happens to be a template. Basically, you'll get the kind of error messages that you are used to from "ordinary functions" and at the time you get them for mistakes with ordinary functions. What's so great about that? I can imagine a time a few years into the future where people won't be able to think of a good answer to that question. This will be the ultimate proof of the success of concepts.

"Concepts" is a type system for types, combinations of types, and combinations of types and integers. Basically, it allows the programmer to state in code what we currently state in documentation and comments. For example, the standard algorithm takes a pair of forward iterators and a value; given that, it assigns the value to each element of the sequence defined by the iterators. That algorithm's requirements on its argument types can be expressed like this:

The checking of use happens immediately at the call site and uses only the declaration. This is a major practical advantage over the current state of affairs for templates where the checking happens late—during template instantiation time.

Concepts also allow us to check the definition of a template in isolations from its arguments:

Again, the error is immediately caught at the point in the definition where it happens.

This checking is the most obvious benefit of concepts, but they also give greatly improved specification of algorithms and class templates and makes overloading straightforward, eliminating the complicated scaffolding of traits and helper functions that people have to rely on today.

Most importantly, concepts help us think about our algorithms and their requirements on their (template) arguments. This leads to more reliable and more general code. Often, such thinking also leads to improved performance as we are forced to think about what the algorithm really needs and what is extraneous.