当我尝试编译以下代码时:
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <cstdlib>
#include <ranges>
#include <vector>
#include <numeric>
#include <cmath>
template <typename T>
struct Foo
{
std::vector<T> vec;
auto begin() { return vec.begin(); }
auto end() { return vec.end(); }
auto begin() const { return vec.cbegin(); }
auto end() const { return vec.cend(); }
auto size() const { return vec.size(); }
};
namespace std::ranges
{
template <typename T>
auto begin(Foo<T>& x) { return x.begin(); }
template <typename T>
auto end(Foo<T>& x) { return x.end(); }
template <typename T>
auto begin(const Foo<T>& x) { return x.begin(); }
template <typename T>
auto end(const Foo<T>& x) { return x.end(); }
}
template <std::ranges::range range>
void some_algorithm(const range& r)
{
return std::accumulate(std::ranges::begin(r), std::ranges::end(r), 0);
}
int main()
{
}
我收到一条错误消息:
include/vector.h:93:78: error: reference to 'end' is ambiguous
93 | return std::sqrt(std::accumulate(std::ranges::begin(range), std::ranges::end(range), 0));
| ^~~
In file included from c:\mingw64\include\c++\10.2.0\string:54,
from c:\mingw64\include\c++\10.2.0\bits\locale_classes.h:40,
from c:\mingw64\include\c++\10.2.0\bits\ios_base.h:41,
from c:\mingw64\include\c++\10.2.0\ios:42,
from c:\mingw64\include\c++\10.2.0\ostream:38,
from c:\mingw64\include\c++\10.2.0\iostream:39,
from main.cpp:1:
c:\mingw64\include\c++\10.2.0\bits\range_access.h:846:42: note: candidates are: 'constexpr const std::ranges::__cust_access::_End std::ranges::__cust::end'
846 | inline constexpr __cust_access::_End end{};
| ^~~
In file included from main.cpp:8:
include/vector.h:71:8: note: 'template<class T, long long unsigned int N> auto std::ranges::end(const vec<T, N>&)'
71 | auto end(const vec<T, N>& v)
“开始”也是如此。我知道错误消息的含义,但我真的不明白为什么会这样。我什至不启动模板,那它为什么还要检查呼叫呢?我有点困惑,有人可以帮助我吗?