C++17: global changes

Exception specification as part of the type system

A prvalue of type “pointer to noexcept function” can be converted to a prvalue of type “pointer to function”. The result is a pointer to the function. A prvalue of type “pointer to member of type noexcept function” can be converted to a prvalue of type “pointer to member of type function”. The result points to the member function.

Example:

Continue reading “C++17: global changes”

C++17: Guaranted copy elision

From C++17 compilers are required to omit copy/move construction resulting in zero-copy pass-by-value semantics. Objects will be created directly in storage where they would copy/move.

  • In return statement when operand is prvalue:

  • In the initialization of a variable, when initialization expression is a prvalue of the same type:

C++17 core language specification of prvalues and temporaries is fundamentally different from that of the earlier C++ revisions: there is no longer a temporary to copy/move from. Another way to describe C++17 mechanics is “unmaterialized value passing”: prvalues are returned and used without ever materializing a temporary.

Continue reading “C++17: Guaranted copy elision”

C++17: Deprecated features

Deprecate redeclaration of static constexpr class members

When inline variables are presented in C++17, static constexpr redeclaration is not needed.

 

Deprecate C library headers

List of C libraries which are deprecated from C++17:

<ccomplex> – simply deleting the requirement for a <ccomplex> header is simpler, and may be better, than making it optional. Of course an implementation would not need to change in order to conform to the new state of affairs, but programs that use <ccomplex> instead of <complex> would cease to be strictly conforming.

<ctgmath> – simply includes header <ccomplex> and <cmath>

<cstdalign> – mentioned as pointless in C++

<cstdbool> – mentioned as pointless in C++

Continue reading “C++17: Deprecated features”

C++17: std::invoke & std::apply

std::invoke

std::invoke is used for calling function objects, like lambdas, methods, functions without knowing about type this function.

For example , before we needed to use pointer to function, pointer to method:

Continue reading “C++17: std::invoke & std::apply”

C++17: std::shared_mutex, interface_sizes, void_t, std::bool_constant, logical operation metafunctions

Class shared_mutex

On some platforms shared_mutex is more effecient then shared_timed_mutex.

In C++17 was added shared_mutex.

Example of usage:

Continue reading “C++17: std::shared_mutex, interface_sizes, void_t, std::bool_constant, logical operation metafunctions”

C++17: Modifications to existing features (Part 2)

Construction for values of fixed enums

Before C++17, variable of fixed enumeration was initialized like this:

if we will try to initialize like this:

we will have error: cannot convert ‘int’ to ‘E’ in initialization.

In C++17 it’s allowed to build.

Continue reading “C++17: Modifications to existing features (Part 2)”

C++17: Modifications to existing features (Part 1)

Single-argument static_assert

In C++17 static_assert message was changed to optional, and it’s possible create static_assert without message.

For example:

Continue reading “C++17: Modifications to existing features (Part 1)”

C++17: Core language features (part 1)

A u8 character literal

In C++17 was added new character literal for UTF-8.

For example:

It’s used for correct translation characters to ASCII on different platforms.

 

Hexadecimal floating point literals

In C++17 was added hexadecimal floating point literals.

Link for cppreference.

Continue reading “C++17: Core language features (part 1)”

C++17: Fold expressions

Fold expressions is the extension for already introduced variadic templates in C++11.

It allows to pack and unpack template parameters in unary and binary operations.

For example:

Example with pushing values:

Continue reading “C++17: Fold expressions”