简体   繁体   English

std::views 尚未声明

[英]std::views has not been declared

I am trying to use the ranges library from c++20 and I have this simple loop.我正在尝试使用 c++20 中的ranges库,并且我有这个简单的循环。

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 .我收到一条错误消息,说error: 'std::views' has not been declared I don't get any errors about including the header.我没有收到关于包含 header 的任何错误。

This is my g++这是我的 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.据我所知,只要你有 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;
}

Depending on the version of your compiler, different standards are the default.根据您的编译器版本,不同的标准是默认的。 For 11.2 it is C++ 17 AFAIK.对于11.2 ,它是 C++ 17 AFAIK。
Cou can just add a flag and it compiles: Cou 可以只添加一个标志并编译:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM