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.

uncaught_exceptions()

New function which returns count of exceptions which were thrown before.

For example:

 

Attributes in namespaces and enumerators

In C++17 it’s allowed to use attributes in namespace and enums.

For example:

 

Attribute namespaces without repetition

In C++17 atributes can be used without repetition namespace.

For example:

Before C++17 it was like this:

 

A variadic version of lock_guard called scoped_lock

In case if need to lock more than one mutex, code will be looks like this:

To simplify code, in C++17 was added scoped_lock, which allows lock more then one lock.

For example:

 

Variable templates for traits

For every standard type trait foo with a single, static member constant foo<Args…>::value, there is now a variable template foo_v<Args…>.

Related standard document: p0006r0.

 

Three-dimensional hypotenuse

In C++17 in <cmath> was added function for hypotenuse in 3-dimensonal space.

For example:

 

Non-const string::data

New method data() which returns non-const data

const CharT* data() const;

CharT* data();

 

Improving pair and tuple

Improved code in std::pair and std::tuple.

Standard related document: n4387.

Constexpr for char_traits

Added constexpr to char_traits.

Related standard document: p0426r1.

 

Changes to <chrono>

Adds floor, ceiling, division and rounding for time points; makes most member functions constexpr.

Related standard document: p0092r1.

shared_ptr for arrays

The class template shared_ptr now supports C-style arrays by passing T[] or T[N] as the template argument, and the constructor from a raw pointer will install an appropriate array deleter.

Related standard document: p0414r2.

shared_ptr::weak_type

The class shared_ptr<T> now has a member type weak_type which is weak_ptr<T>. This allows generic code to name the corresponding weak pointer type without having to destructure the shared pointer type.

Related standard document: p0163r0.

atomic::is_always_lock_free

A new static member constant is_always_lock_free that documents whether the operations of a given atomic type are always lock-free. The existing non-static member function is_lock_free may give different answers for different values of the atomic type.

Improved insertion for unique-key maps

 Two new methods:

insert_or_assign() – do not needs default constructor of the mapped type and returns more information, then operator[]. Method returns pair of reference and bool value which indicates, object was inserted or not.

try_emplace(key, arg1, arg2, arg3)  – does nothing if key already exists in the map, and otherwise inserts a new element constructed from the arguments. This interface guarantees that even if the arguments are bound to rvalue references, they are not moved from if the insertion does not take place.

Return type of emplace

Sequence containers whose emplace{,_front,_back} member function templates used to return void now return a reference to the newly inserted element. (Associative containers are not affected, since their insertion functions have always returned iterators to the relevant element.)

Splicing maps and sets

A new mechanism, node handles, has been added to the container library that allows transplanting elements between different map/set objects without touching the contained object. Moreover, this technique enables mutable access to key values of extracted nodes.

Related standard document: p0083r3.

Incomplete type support for allocators

This change relaxes the requirements on allocators to have complete value types, and allows, for example, recursive structures like:

Related standard document: n4510.

One Reply to “C++17: Modifications to existing features (Part 2)”

  1. Write more, thats all I have to say. Literally, it seems as though
    you relied on the video to make your point. You obviously know what youre talking about, why waste your
    intelligence on just posting videos to your site
    when you could be giving us something informative to read?

Leave a Reply

Your email address will not be published.