Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ADL不在室外工作(即使是结构)_C++_Scope_Overloading_Overload Resolution_Argument Dependent Lookup - Fatal编程技术网

C++ ADL不在室外工作(即使是结构)

C++ ADL不在室外工作(即使是结构),c++,scope,overloading,overload-resolution,argument-dependent-lookup,C++,Scope,Overloading,Overload Resolution,Argument Dependent Lookup,如果我有一个结构,重载开始,结束,等等,如下所示: #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 e

如果我有一个结构,重载
开始
结束
,等等,如下所示:

#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);
}
#包括
模板
结构阵列10
{
私人:
std::数组m_数组;
公众:
constexpr自动开始()
{
返回std::begin(m_数组);//1
}
constexpr自动结束()
{
返回std::end(m_数组);
}
//cbegin、cend、rbegin、rend、crbegin、CRND
};
int main()
{
阵列10 arr;
标准:填充(
标准::开始(arr),/#2
标准::结束(arr),
0);
}
然后我明白了为什么我必须在#1中使用
std::begin
而不是
begin
(因为这是#2中定义的最接近的范围
begin
),但我不明白为什么我必须在#2中使用它。 我的第一个猜测是因为
Array10
main
在同一个名称空间中,但将其放入自己的名称空间并不能解决问题


那么:为什么ADL没有在
main
(#2)中找到
std::begin

ADL没有找到成员。它可以找到自由函数

您有一个成员
foo.begin()
,而不是一个自由函数
begin(foo)


这些是非成员的
begin
函数,可以通过ADL找到。

实际上,只是使用std::begin放置
等也可以使用,而且更简单。
friend constexpr auto begin(Array10&)
{
    return std::begin(m_array); // #1
}
friend constexpr auto begin(Array10 const&)
{
    return std::begin(m_array); // #1
}