BT

InfoQ Homepage News C++20 Is Now Final, C++23 at Starting Blocks

C++20 Is Now Final, C++23 at Starting Blocks

Bookmarks

Originally planned for release last February, C++20 has now received the final technical approval and will be published foreseeably by the end of the year. C++20 will include modules, coroutines, and concepts among its major new features.

Modules introduce a new way to encapsulate code which also aims to make header files unnecessary. The significance of modules cannot be downplayed, wrote Herb Sutter:

This is the first time in about 35 years that C++ has added a new feature where users can define a named encapsulation boundary.

Along with variables, functions, and classes, modules provide a way for programmers to name an entity which hides some kind of implementation. Until now, variables could be used to encapsulate values and functions to encapsulate behaviour, while classes encapsulate state and behaviour. Modules offer a mechanism to encapsulate variables, functions, and classes all together.

That’s a fundamental reason underlying why modules enable further improvements that can now follow on in future C++ evolution.

This is how you can define a simple module exporting a function and its usage in a different file:

// math.cppm
export module mod1;

export int identity(int arg) {
    return arg;
}

// main.cpp

import mod1;

int main() {
  identity(100);
}

Coroutines are stack-less functions that can be stopped and resumed. Coroutines support three operators that can be used to create asynchronous tasks, generators, and lazy functions: co_await to suspend execution without returning a value to the caller; co_yield to suspend while returning a value; and co_return to finalize the execution and return a value. The following is an example of generator:

generator<int> iota(int n = 0) {
  while(true)
    co_yield n++;
}

Concepts are an extension of templates that enable the definition of compile-time predicates on template parameters. Concepts are meant to model semantic categories rather than syntactical restrictions. The standard library already includes a number of pre-defined concepts such as Integral, Assignable, StrictTotallyOrdered, MoveConstructible, Callable, and many others. For example, the Integral concept is defined like this:

template <class T>
concept bool Integral() {
    return std::is_integral<T>::value;
}

Once you have a concept defined, you can use it in a template definition like this:

template<Integral T>   // shorthand for: template<typename T> requires Integral<T>()
T foo(T a, T b) {
  ...
}

Integral foo(Integral&);  // even shorter

C++20 also includes a number of smaller changes, including structured bindings extension and reference capture, aggregates initialization using (), polymorphic_allocator, and more.

While C++20 is making its final way to become the current C++ standard, the ISO committee has already started working on C++23, which could feature a modular standard library, executors, reflection, pattern matching, and contracts, although the current pandemics and restrictions to face-to-face meeting are presumably going to reduce the amount of work that can be done.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.