C++17: new types: std::any, std::optional, std::variant

std::any

Any class can hold any type of object.  It’s type safe container for single values of any type.  A better way to handle any type and replace void*.

Type requeirments:

std::decay_t<T> – must be CopyConstructable

Example:

To get value from std::any you need to use std::any_cast.

In case if type in std::any_cast is wrong will be called std::bad_any_cast exception.

Continue reading “C++17: new types: std::any, std::optional, std::variant”

C++17: std::string_view

std::string_view it’s class which can view std::string or const char* without ownership and without additional memory requesting.

SSO (Small string optimization)

std::string has optimization for small strings ( compiler specific feature, for gcc, clang and msvc there are different values ).

For example for GCC 7.3 this value is 16 bytes. If string has <=16 bytes it will be saved in stack inside std::string object. If more then 16 bytes, then data used for saving string will be in heap. Which means additional memory allocations.

It can be checked by overloading operator new in global space.

Continue reading “C++17: std::string_view”

C++17: fixed issues

In C++17 there is couple changes to behavior of things which were already in C++.

1 ) auto x{ 1 } – now it’s int value, not initializer_list.

For a braced-init-list with only a single element, auto
deduction will deduce from that entry;

For a braced-init-list with more than one element, auto
deduction will be ill-formed.

For example:

Standard paper related to this changes: n3922.

Continue reading “C++17: fixed issues”

C++17 Parallel STL (7 new algorithms)

Parallel algorithms implementation it’s a new feature of C++17 standard. Which allow to use simple STL functions in multiple threads.

At the moment (17.07.2018) only one compiler supports Parallel Algorithms. It’s Visual Studio 2017 ( versions 15.3 and higher).

To build C++ projects with parallel STL you need to build with a flag:

/std:c++17

In C++17 we have a new term – Execution policy

It’s the first parameter to parallel functions from STL. It allows you to choose, how can be function executed. To use it you need to include

#include <execution>

Types of execution policy:

std::seq – without using threads or vectorization, sequenced instructions. This is a default value.

std::par – parallel execution with using threads. When using parallel execution policy, it is the programmer’s responsibility to avoid deadlocks.

std::par_unseq – parallel execution using threads and vectorization or migrating across threads(such as by parent-stealing scheduler). Functions with this policy are permitted to execute in unordered fashion in unspecified threads and unsequenced with other threads.

Continue reading “C++17 Parallel STL (7 new algorithms)”

Removed features from C++17

1 – Trigraphs

Trigraphs were removed from C++17 because they are not needed anymore. They were used in C/C++ in 80-s. Because the old coding table not supported all needed symbols ISO/IEC646 like:  # ,  [ ,  ]. etc.

Trigraph Equivalent symbol
??= #
??/ \
??’ ^
??( [
??) ]
??! |
??< {
??> }
??- ~

Also even before C++17 GCC compiler show warning when trigraphs were used, because using trigraphs can turn to unexpected behavior, for example:

Continue reading “Removed features from C++17”