At its winter meeting in Kona, HI, USA, the ISO C++ committee has finalized work on C++17, writes Herb Sutter. Here is a short summary of its main features.
The Kona meeting was mostly aimed to address review comments from national bodies and to fix bugs, so it did not bring many new features. In particular, a new std::byte
type has been added at Kona to streamline the way programs deal with byte-oriented access to memory. This is currently done using either the char
, signed char
, or unsigned char
types, which is less than ideal since those types support char and arithmetic operations. On the other hand, std::byte
will be a separate, dedicated type.
Other significant new features in C++17 include:
-
Lambdas can be used in
constexpr
s, thus removing a preexisting limitation.constexpr
allows to declare objects or functions that can be evaluated, e.g., at compile time, making it possible to provide advanced template parameters or array-size specifiers. The compiler will ensure that aconstexpr
object or function complies with given requirements to make possible its use as described. -
Capturing
*this
as a value in lambdas is now allowed, which makes a big difference in parallel code or withstd::future
, since there is no guarantee thatthis
is still valid when the lambda executes. -
Optionals are available through
std::optional
to handle the representation of a value that can be defined or not. In C++ parlance, this means that an optional is a wrapper that manages the initialization state of an object. -
Union types are available through
std::variant
, which allow to define a type like “float or long integer”. -
Constructors can deduce the template arguments of the class, making it possible to just write:
auto lock = std::lock_guard(mut_, r1);
instead of:
std::lock_guard<std::shared_timed_mutex, std::shared_lock<std::shared_timed_mutex>> lck(mut_, r1);
-
Templates can infer their non-type parameters, e.g.:
template <auto v> struct S; // type of v is deduced`
The list of new features in C++17 is sufficiently long to not be possible to list all of them here. This answer on Stack Overflow provides a succinct summary, complete up to the latest meeting in Kona, including pointers to the official committee documents.