Search code examples
c++scopeoverloadingoverload-resolutionargument-dependent-lookup

ADL not working outside (even of structure)


If I have a structure the overloads begin, end, etc. like the following:

#include <array>

template<typename T>
struct Array10
{
private:
    std::array<T, 10> m_array;
public:
    constexpr auto begin()
    {
        return std::begin(m_array); // #1
    }

    constexpr auto end()
    {
        return std::end(m_array);
    }

    // cbegin, cend, rbegin, rend, crbegin, crend
};

int main()
{
    Array10<int> arr;
    std::fill(
    std::begin(arr), // #2
    std::end(arr),
    0);
}

then I understand why I have to use std::begin instead of begin in #1(because that's the closest scope begin is defined in), but I don't understand why i have to use it in #2. My first guess would be because Array10 is in the same namespace as main, but putting it into its own namespace didn't solve the problem.

So: Why does ADL not find std::begin in main(#2)?


Solution

  • ADL doesn't find members. It finds free functions.

    You have a member foo.begin(), not a free function begin(foo).

    friend constexpr auto begin(Array10&)
    {
        return std::begin(m_array); // #1
    }
    friend constexpr auto begin(Array10 const&)
    {
        return std::begin(m_array); // #1
    }
    

    those are non-member begin functions that will be found via ADL.