Do we have support for C++20 ranges library in the newly released GCC 9?
I copied the example code below for ranges library from: https://en.cppreference.com/w/cpp/ranges
#include <vector>
#include <ranges>
#include <iostream>
int main()
{
std::vector<int> ints{0,1,2,3,4,5};
auto even = [](int i){ return 0 == i % 2; };
auto square = [](int i) { return i * i; };
for (int i : ints | std::view::filter(even) | std::view::transform(square)) {
std::cout << i << ' ';
}
}
But when compiled with g++ 9.1 (Ubuntu 18.04 LTS (Bionic Beaver)), it complains that <ranges>
cannot be found:
$ g++ -std=c++2a cpp2a.cpp
cpp2a.cpp:2:10: fatal error: ranges: No such file or directory
2 | #include <ranges>
| ^~~~~~~~
compilation terminated.
Am I missing something?
And will the ranges library arrive at some point of time with the GCC 9 series?
Am I missing something?
No.
And will the ranges library arrive at some point of time with the gcc-9 series?
It's possible but seems unlikely. This did not happen. The first release to support Ranges in gcc was gcc 10.1.
Ranges is an enormous library. It's still 2019, the official C++20 standard still won't even be shipped for another year and a half. It'll take a while for it to get implemented in the major standard library implementations. We'll just have to wait.
If you want to start using Ranges, you can use Range-v3 (specifically the v1.0-beta
branch) or you can find an implementation of C++20 Ranges at cmcstl2 (this is Casey Mysterious Carter's implementation).
You can also periodically check cppreference's compiler/library tracking page (which at the time of writing shows no libraries having implemented the One Ranges proposal, but nevertheless does show quite a few C++20 features as having been implemented by the various library vendors).
Per Table 1.7. C++ 2020 Implementation Status
The One Ranges Proposal P0896R4
the version is 10.1. That means that you'll need to upgrade to at least that version to get the feature.
g++10 supports <ranges>
feature along with a number of other c++20 features .
#include <vector>
#include <ranges>
#include <iostream>
int main()
{
std::vector<int> ints{0,1,2,3,4,5};
auto even = [](int i){ return 0 == i % 2; };
auto square = [](int i) { return i * i; };
for (int i : ints | std::views::filter(even) | std::views::transform(square)) {
std::cout << i << ' ';
}
}
$ g++-10 -Wall -Wextra -std=c++20 ranges.cpp
$ ./a.out
0 4 16
On Ubuntu, the packages are now available. To install:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt install gcc-10 g++-10
However, g++-10 does not have support for all features. See the Table 1.8. C++ 2020 Library Features for features list supported.
GCC 10 has been released as stable in March (with a version update 10.1 on May 7, 2020 ).
This version supports ranges
.
The compiler/library tracking page is up to date as well.
Thus your code sample* compiles: https://godbolt.org/z/MPxBMs
#include <vector>
#include <ranges>
#include <iostream>
int main()
{
std::vector<int> ints{0,1,2,3,4,5,6};
auto even = [](int i){ return 0 == i % 2; };
auto square = [](int i) { return i * i; };
for (int i : ints | std::views::filter(even) | std::views::transform(square)) {
std::cout << i << ' ';
}
}
*: with a small fix, since the namespace alias is std::views
not std::view
.
I tested this with a local installation compiled by hand on macOS.
g++-10.1 -std=c++20 -Wall test.cpp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.