Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c++ - 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)?

question from:https://stackoverflow.com/questions/65925200/adl-not-working-outside-even-of-structure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...