when I try to compile following code:
// 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()
{
}
I get an error message:
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)
The same for "begin". I know what the error message means, but I dont really understand why it is happening. I dont even initiate the template, so why should it even check for the call? Im a little bit confused, can someone help me?