Bjarne Stroustrup mines generic programming for a better C++

Bjarne Stroustrup mines generic programming for a better C++

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.”

IDG Insider


«AirPods wish list: 3 ways Apple can make its Bluetooth earphones even better


CIA updates rules for collecting and retaining info on US people»
IDG Connect

IDG Connect tackles the tech stories that matter to you

Our Case Studies

IDG Connect delivers full creative solutions to meet all your demand generatlon needs. These cover the full scope of options, from customized content and lead delivery through to fully integrated campaigns.


Our Marketing Research

Our in-house analyst and editorial team create a range of insights for the global marketing community. These look at IT buying preferences, the latest soclal media trends and other zeitgeist topics.



Is your organization fully GDPR compliant?