GCC 14 is going to be the system compiler of Fedora 40 and RHEL 10. Lots of exciting new C++23 and C++23 library features have been implemented in GCC 14. This blog post aims to provide a brief overview of the main new features.
std::ranges::to
The C++23 function std::ranges::to can be used to create a container out of a given range. For example,
auto r = std::views::iota(1, 10);
// create a vector from the elements of l, i.e. {1,2,...,9};
auto vec = std::ranges::to<std::vector<int>>(r);
// create a list from the elemnets of vec
auto list = std::ranges::to<std::list<int>>(vec);
std::print and std::println
Say goodbye to std::cout! With std::print (and std::println), one can conveniently and efficiently print formatted text directly to stdout et al.
std::println("Hello {}!", "World!);
// Previously needed to do
// std::cout << std::format("Hello {}!", "World") << std::endl;
std::generator
The new `<generator>` header provides an `std::generator<T>` class template that can be used to generate a potentially infinite sequence of elements from a coroutine. This sequence is effectively a C++20 input range, and so can be consumed by views and ranges algorithms. For example,
std::generator<int> fibonacci() {
int a = 0;
int b = 1;
for (;;) {
co_yield a;
int c = b;
b = a + b;
a = c;
}
}
void f() {
auto v = fibonacci() | std::views::take(6);
// v is [0,1,1,2,3,5]
}
Saturating arithmetic
The arithmetic functions `std::add_sat`, `std::sub_sat`, `std::mul_sat`, `std::div_sat` and `std::saturate_cast` that saturate on overflow (instead of wrapping, in the unsigned case, or invoking UB, it the signed case) have been implemented.
int x = sub_sat(1u, 5u); // x is 0
Other enhancements
- Formatters for std::thread_id and std::stacktrace have been added.
- std::to_string has been optimized to use std::format.
- The std::text_encoding class has been implemented.
Conclusion
GCC 14 builds upon the numerous new C++ features and enhancements from GCC 13 to ensure it's the best version yet. Aside from new features, many bugs have been fixed as well. GCC 14 is slated to be released in May 2024.