简体   繁体   中英

c++20 infinite ranges/views compiler error

So I watched a video (14:30) about the new c++ 20 features and wanted to try out infinite ranges.

I wrote this code:

#include <iostream>
#include <cmath>
#include <ranges>

using std::cout;
using std::endl;

bool primeCheck(const int&);

int main()
{
    auto ints = std::ranges::view::ints(0);
    auto primes {
        ints
        | std::ranges::views::filter([](const auto& value){ return primeCheck(value); })
        | std::ranges::views::take(100)
    };

    for (auto p : primes)
    {
        cout << p << endl;
    }
    return 0;
}

bool primeCheck(const int& value)
{
    if (value <= 1) return false;
    for (int i = 2; i <= round(sqrt(value)); i++)
    {
        if (value % i == 0)
        {
            return false;
        }
    }
    return true;
}

However I get the following compiler output:

/bin/sh -c '/usr/bin/make -j4 -e -f  Makefile'
----------Building project:[ InfiniteSequences - Release ]----------
make[1]: Entering directory '/home/paul/Documents/Projects/InfiniteSequences'
/usr/bin/g++-10  -c  "/home/****/Documents/Projects/InfiniteSequences/main.cpp" -std=c++20 -Wall -Wextra -Wpedantic -Wdouble-promotion -Wformat=2 -Wformat-nonliteral -Wformat-signedness -Wformat-y2k -Wnull-dereference -Wimplicit-fallthrough=2 -Wmissing-include-dirs -Wswitch-default -Wunused-parameter -Wuninitialized -Wsuggest-attribute=const -Walloc-zero -Walloca -Wconversion -Wfloat-conversion -Wsign-conversion -Wduplicated-branches -Wduplicated-cond -Wtrampolines -Wfloat-equal -Wshadow=compatible-local -Wundef -Wunused-macros -Wcast-qual -Wcast-align=strict -Wlogical-op -Wmissing-declarations -Wredundant-decls -Wstack-protector -fstack-protector -pedantic-errors -Werror=pedantic -Werror=char-subscripts -Werror=null-dereference -Werror=init-self -Werror=implicit-fallthrough=2 -Werror=misleading-indentation -Werror=missing-braces -Werror=multistatement-macros -Werror=sequence-point -Werror=return-type -Werror=multichar -DNDEBUG  -o ./Release/main.cpp.o -I. -I.
/home/****/Documents/Projects/InfiniteSequences/main.cpp: In function 'int main(int, char**)':
/home/****/Documents/Projects/InfiniteSequences/main.cpp:14:30: error: expected 'auto' or 'decltype(auto)' after 'view'
   14 |     auto ints = std::ranges::view::ints(0);
      |                              ^~~~
/home/****/Documents/Projects/InfiniteSequences/main.cpp:14:30: error: 'template<class _Tp> concept std::ranges::view' used without template arguments
make[1]: *** [InfiniteSequences.mk:95: Release/main.cpp.o] Error 1
make[1]: Leaving directory '/home/paul/Documents/Projects/InfiniteSequences'
make: *** [Makefile:5: All] Error 2
====2 errors, 0 warnings====

I looked it up again from a different source (page 6) and it should work, I think.

Use

auto ints = std::ranges::views::iota(0);

For some reason the video listed it as view::ints and the pdf source as view::iota, both of which are wrong.

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.

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