Coming in C++20
The main features in C++20 of interest to scientific computing are:
Modules
Concepts
Views
C++ 20 introduces the ranges library. This allows us to more easily consider views into our containers.
Here’s an example of using a range-based for loop over a set of integers:
#include <iostream>
#include <ranges>
int main() {
for (auto i : std::views::iota(2, 7)) {
std::cout << i << std::endl;
}
}
Range Adaptors
Range adaptors look like pipes that we saw when discussing Bash (see: https://learn.microsoft.com/en-us/cpp/standard-library/range-adaptors?view=msvc-170)
Here’s an example of using an adaptor to reverse the iteration through a vector using a range-based for loop:
#include <iostream>
#include <vector>
#include <ranges>
int main() {
std::vector<double> v{1, 2, 3, 4, 5};
for (auto e : v | std::views::reverse) {
std::cout << e << " ";
}
std::cout << std::endl;
}
3-way Comparison
<numbers>
The numbers header provides mathematical constants. They are implemented as templates that can be defined with whatever type you need:
#include <iostream>
#include <iomanip>
#include <limits>
#include <numbers>
int main() {
std::cout << std::setprecision(std::numeric_limits<float>::digits10+1)
<< std::numbers::e_v<float> << std::endl;
std::cout << std::setprecision(std::numeric_limits<double>::digits10+1)
<< std::numbers::e_v<double> << std::endl;
std::cout << std::setprecision(std::numeric_limits<long double>::digits10+1)
<< std::numbers::e_v<long double> << std::endl;
}
<format>
We already saw the new style formatting when we looked at std::print()
.
The format is handled by the format header.