因此,我观看了一段关于 c++ 20 新特性的视频(14:30),并想尝试无限范围。
我写了这段代码:
#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;
}
但是我得到以下编译器输出:
/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====
我从另一个来源(第 6 页)再次查找它,我认为它应该可以工作。