【发布时间】:2020-09-21 00:18:53
【问题描述】:
我正在构建一些适用于 C++20 Ranges 库和 range-v3 库的 sn-ps,我注意到 copy 算法的实现存在一些差异。
以下代码使用 C++20 范围库(来自标准库),使用 GCC 11 编译(并打印 1 2 3 4 5)。
#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>
int main()
{
std::vector<int> arr {1, 2, 3, 4, 5};
std::ranges::copy(arr, std::ostream_iterator<int>(std::cout, " "));
}
这是一个链接:https://wandbox.org/permlink/V13bdDoxSYjqDW3m
使用 range-v3 库的相同代码无法使用 VC++ 2019 16.5 进行编译:
#include <iostream>
#include <vector>
#include "range/v3/algorithm/copy.hpp"
int main()
{
std::vector<int> arr {1, 2, 3, 4, 5};
ranges::copy(arr, std::ostream_iterator<int>(std::cout, " "));
}
出现以下错误:
main.cpp(134,9): error C2672: 'operator __surrogate_func': no matching overloaded function found
main.cpp(134,59): error C7602: 'ranges::_copy::copy_fn::operator ()': the associated constraints are not satisfied
range-v3-master\include\range/v3/algorithm/copy.hpp(57): message : see declaration of 'ranges::_copy::copy_fn::operator ()'
main.cpp(134,59): error C2780: 'ranges::detail::in_out_result<I,O> ranges::_copy::copy_fn::operator ()(I,S,O) const': expects 3 arguments - 2 provided
range-v3-master\include\range/v3/algorithm/copy.hpp(45): message : see declaration of 'ranges::_copy::copy_fn::operator ()'
range-v3库中有一个unit test,非常相似:
using namespace ranges;
std::ostringstream sout;
std::vector<int> copy_vec{1,1,1,1,1};
copy(copy_vec, ostream_iterator<>(sout, " "));
CHECK(sout.str() == "1 1 1 1 1 ");
如果我尝试使用编译器资源管理器,它不会使用任何编译器(gcc、Clang、VC++)进行编译。 gcc 错误是:
note: candidate expects 1 argument, 2 provided
note: candidate expects 3 arguments, 2 provided
error: no match for call to '(const ranges::copy_fn) (std::vector<int>&, std::ostream_iterator<int>)'
required from here
这些与我在 VC++ 中看到的 sn-p 错误基本相同。
这是一个链接:https://godbolt.org/z/pEfBb4
我希望这两者可以互换使用。为什么不是这样?
【问题讨论】:
标签: c++ gcc visual-c++ c++20 range-v3