Classy move: C++ 20 wins final approval in ISO technical ballot, formal publication expected by end of year
'Best approximation of C++ ideals so far,' says Stroustrup - but is it too big and complex?
C++ 20, the latest version of the venerable object-oriented programming language, has been unanimously endorsed in ISO's final technical approval ballot.
It awaits a "final round of editorial work," following which it will be formally published, expected to be "towards the end of 2020."
A major new version of C++ comes every three years, named after the year, so C++ 20 is the successor to C++ 17. C++ 20 is a big release, bigger than its predecessors. There are four key new features often called the “big four.” These are:
- Modules. Source code that gets compiled once and then reused as a binary file. They are a faster alternative to header files, isolate the code more, and will improve compile times.
Bjarne Stroustrup, creator of C++, spoke about C++ 20 a year ago at Cppcon in Aurora, Colorado, remarking that the language had reached the human equivalent of middle age. He singled out modules as one of the two most significant features in C++ 20, because they “should improve our compile times by 5 to 10 times” – but only if developers use them to clean up their code.
Another big feature, and the second one which Stroustrup highlighted, is called Concepts. “A concept is a compile-time predicate,” he explained in 2016. “For example, if a template takes a type argument, we can specify that an argument, T, can be an iterator, Iterator<T>, a random access iterator, Random_access_iterator<T>, or maybe a number, Number<T> … Concepts enable overloading and eliminate the need for a lot of ad-hoc metaprogramming.”
- Ranges library. Dependent on Concepts, Ranges abstract collections.
- Coroutines, functions that “can suspend execution to be resumed later,” used for asynchronous programming.
There are many other new features, including the “spaceship operator” <=> which does a three-way comparison and returns less, equal or more.
Contracts, a feature once slated for C++ 20, has been moved out of this release and into a study group, as they were not ready.
C++ is an evolving language. Stroustrup said that “C++ 23 will complete C++ 20,” adding standard modules, library support for coroutines, Executors (a key feature for parallelism) and plenty more.
Is C++ becoming too large and complex?
It is an intimidating language, but the effect of the new features should be to simplify C++ programming. In his Cppcon talk, Stroustrup said that C++ 20 is “the best approximation of C++ ideals so far”, and that C++ 23 will be better still.
Stroustrup resisted efforts to describe the language in terms of the new features added with each major release. “The language is not a layer cake,” he said.
He has advocated starting with the “simple and elegant features” and not worrying about parts of the language which may be there only for backward compatibility, which he said was critically important. The burden of backward compatibility falls on implementers rather than users, and that is how it should be, said Stroustrup, because there are many thousands of users but only a few implementers.
Bjarne Again: Hallelujah for C++READ MORE
One of the goals, according to Stroustrup, is to reduce the need for template metaprogramming. “Template metaprogramming was a wildly successful C++ 98 feature … the fact that it was so heavily used, even though it was so ugly and difficult, is proof of its utility. We have to make it easier to do the things that were done in template metaprogramming,” he said. Features including Constexpr functions, in C++ 11, and Concepts in C++ 20, help with this, he said.
Good C++ code should be easy to understand, according to Stroustrup. “One of the measures of good code is that I can understand it,” he said.
The major C++ compilers do not yet implement everything in C++ 20. A list of what is implemented in Clang is here, and GCC here (no Modules support yet). The latest Visual C++ supports some C++ 20 features when used with flags such as /experimental:module and /std:c++latest. A table of compiler conformance status is here.
C++ ranks as the fifth most popular programming language in the Redmonk language rankings, and is – if anything – creeping up. In 2012, when these rankings were first calculated, it took seventh place. Despite the attention devoted to more recent languages such as Rust and Go, C++ is here to stay. ®