Algorithms & Execution Policies
-
Most functions in
<algorithm>were respecified in C++17- Add an optional argument for the execution policy
-
Some algorithms were naturally sequential
- e.g.
equal_range()
- e.g.
-
These were left unchanged
-
Some algorithms in
<numeric>now have two versions- The C++14 version
- A new version with policy support
-
The new versions have new names
accumulate() -> reduce()partial_sum() -> inclusive_scan(), exclusive_scan()
-
There is also a new "fused" algorithm with policy support
transform() + inner_product() -> transform_reduce()
Algorithms and Exceptions in C++14
-
Algorithms can throw exceptions
- e.g. an algorithm call which applies a function to every element
- The function throws an exception
-
The exception will be handled by other code
- If there is no handler, execution ends
std::vector<int> vec{3, 1, 4, 1, 5, 9};
try {
// Predicate throws an exception
std::sort(vec.begin(), vec.end(), [](int a, int b) {
throw std::out_of_range("oops");
return true;
});
} catch (std::exception& e) {
std::cout << "Caught exception: " << e.what() << '\n';
}
Algorithms and Exceptions in C++17
- This approach does not work with execution policies
- May be multiple threads
- Each thread has its own execution stack
- If an exception is thrown,
std::terminate()is called