3

I am trying to use the ranges library from c++20 and I have this simple loop.

for (const int& num : vec | std::views::drop(2)) {
    std::cout << num << ' ';
}

I get an error message saying error: 'std::views' has not been declared. I don't get any errors about including the header.

This is my g++

g++.exe (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.

As far as I know it should work as long as you have c++20.

The following example does not compile on my machine:

#include <iostream>
#include <ranges>
#include <vector>

int main() {
    std::cout << "Hello world!";
    std::vector <int> vec = {1, 2, 3, 4, 5};
    // print entire vector
    for (const int& num : vec) {
        std::cout << num << ' ';
    }
    std::cout << "\n";
    // print the vector but skip first few elements
    for (const int& num : vec | std::views::drop(2)) {
        std::cout << num << ' ';
    }
    return 0;
}
Enlico
  • 23,259
  • 6
  • 48
  • 102
wyvern
  • 43
  • 1
  • 6
  • Btw i got my g++ from here: https://winlibs.com/ and I'm following the example here https://en.cppreference.com/w/cpp/ranges/drop_view. On their online compiler that runs gcc 11.1 it runs fine – wyvern Dec 18 '21 at 13:07
  • Did you include the ranges header? – Oli L Dec 18 '21 at 13:18
  • Compiler support of C++20 is still quite poor. Lost of things do not work yet, some features are available on one compiler and other on different one. Yor example works with gcc 11.2 https://godbolt.org/z/Gdx18j5sW but I'm not sure what is the status of Windows port of it. – Marek R Dec 18 '21 at 13:20
  • 1
    Please provide [mcve] with complete code and complete build process, since it is possible that you mistyped something or forgot to add proper compiler flag. Note in previous comment I've provided minimal complete reproducible example when it works. – Marek R Dec 18 '21 at 13:22
  • 1
    @OliL yes I did – wyvern Dec 18 '21 at 14:02
  • @MarekR that would explain it. Kinda sucks i think ranges are a great feature – wyvern Dec 18 '21 at 14:03
  • That is the only build for g++ 11.2 I could find and download on windows. – wyvern Dec 18 '21 at 14:11
  • @wyvern initially ranges were implemented as third party library for C++14. You can use it, it is fully functional. Since library is great it was incorporated into C++20. – Marek R Dec 18 '21 at 14:26
  • I just tested it with clang as well, same error on windows. Using it as a third party library isn't an option for me right now, but I'll use that for any personal projects, thanks – wyvern Dec 18 '21 at 15:24

1 Answers1

5

Depending on the version of your compiler, different standards are the default. For 11.2 it is C++ 17 AFAIK.
Cou can just add a flag and it compiles:

g++ --std=c++20 main.cc

  • You're right, it does compile! However for some reason I can't run it. I think I'm doing something wrong – wyvern Dec 18 '21 at 16:39
  • Okay so, idk why g++ isn't working, but with `-std=c++20` i was able to get it to compile with clang so that's great! Thanks for your answer! – wyvern Dec 18 '21 at 16:46