-1

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?

cpplearner
  • 13,776
  • 2
  • 47
  • 72
user3520616
  • 60
  • 3
  • 17

1 Answers1

2

std::ranges::begin is not a function; it is a function object. You cannot specialize it or redefine it. This is deliberate.

The old standard method of specializing begin/end in the std namespace is not how Ranges works, and it was designed specifically to stop you from doing that. The way you make a range is by defining member begin/end functions or by defining ADL-capable begin/end functions in the namespace of the range type.

Similarly, the silly using std::begin; begin(rng); idiom is not how you invoke ranges::begin. You invoke it by... calling std::ranges::begin. Because that's how you ought to invoke a function.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982