Bjarne Stroustrup mines generic programming for a better C++

The creator of C++ sees concepts in generic programming as key to more efficient, reliable code

Bjarne Stroustrup is on a mission to simplify generic programming.

In a recently published paper titled "Concepts: The Future of Generic Programming," Stroustrup makes the case for concepts as a foundation for generic programming. In concepts, Stroustrup sees the solution to the interface specification problem that has long dogged C++, the language he founded more than 35 years ago.

“The way we write generic code today is simply too different from the way we write other code,” Stroustrup says.

Stroustrup defines generic programming as programming focused on design and implementation of general algorithms, with algorithms capable of accepting a variety of types. Generic programming can also be used to write efficient libraries. Stroustrup hopes to see his ideas come to fruition in C++ in fewer than three years. Concepts, Stroustrup believes, will greatly ease engineers’ ability to write efficient, reliable C++ code.

Currently an ISO technical specification, concepts provide well-specified interfaces to templates without runtime overhead. Concepts, Stroustrup writes, are intended to complete C++’s support for generic programming as initially envisioned. “The purpose of concepts is to fundamentally simplify and improve design. This leads to fewer bugs and clearer -- often shorter -- code.”

The most obvious effect will be a massive improvement in the quality of error messages, but the most important long-term effect will be found in the flexibility and clarity of code, Stroustrup says. “In particular, having well-specified interfaces allows for simple, general and zero-overhead overloading of templates. That simplifies much generic code.”

Concepts also address the long-standing problem of template syntax being too heavy. Stroustrup’s solution will allow programmers to test the type of template arguments against user-defined compile-time predicates.

Concepts currently are available as part of GNU C Compiler 6.2. Stroustrup would like to see them be part of C++ 20.

“I hope all major C++ implementations will support concepts well before 2020. IMO, concepts should have been part of C++ 17, but the committee couldn’t reach consensus on that,” Stroustrup writes.

Once concepts are in the standard working paper, they can then be used to simplify the standard library and to introduce Ranges, a modernized version of the standard library featuring concepts.

Concepts are meant to complete C++’s support for generic programming as initially envisioned.

Stroustrup recalls what he had had in mind: “I wanted three properties for templates: full generality/expressiveness, zero overhead compared to hand coding [and] well-specified interfaces.”

But no one could figure out how to get all three, so C++ ended up with Turing completeness and better-than-hand-coding performance but lousy interfaces -- basically compile-time duck typing, in which a function accepts any type that has the properties that the implementation happens to use, instead of checking types in interfaces. This situation can result in complicated code.

While Stroustrup focuses on C++, his solution could be applied to other languages.

“The basic ideas are very general,” Stroustrup writes. “The idea of applying a compile-time predicate can be applied to any compiled language, and many languages offer some variant of the idea.”

Join the TechWorld newsletter!

Error: Please check your email address.

More about ConceptsISO

Show Comments