基于范围的 For 循环和 ADL

Range-Based For Loop and ADL

这是 2011 年对这个问题的扩展: Range-based for loops and ADL

使用 Visual Studio 2015,我无法使用参数相关查找 (ADL) 为自定义容器创建基于范围的 for 循环。

我在下面用自定义容器做了一个非常简单的测试用例:

#include <vector>

namespace Foo
{
    template <typename T>
    class Container
    {
    public:

        std::vector<T> values;
    };
}

template <typename T>
typename std::vector<T>::iterator begin(Foo::Container<T>& foo)
{
    return foo.values.begin();
}

template <typename T>
typename std::vector<T>::iterator end(Foo::Container<T>& foo)
{
    return foo.values.end();
}

使用这个容器和 ADL,下面的测试编译得很好:

int main(int argc, char* argv[])
{
    Foo::Container<int> values;

    for (auto it = begin(values); it != end(values); ++it)
    {
        ...
    }

    return 0;
}

理应如此。我不确定这里是否甚至使用了 ADL,但无论如何,它是有道理的。从 MSDN documentation,我们有:

Keep in mind these facts about range-based for:

  • Automatically recognizes arrays.

  • Recognizes containers that have .begin() and .end().

  • Uses argument-dependent lookup begin() and end() for anything else.

根据我对 ADL 的理解以及上面的文档,以下内容也应该编译:

int main(int argc, char* argv[])
{
    Foo::Container<int> values;

    for (auto value : values)
    {
        ...
    }

    return 0;
}

但事实并非如此。相反,我收到以下错误:

error C3312: no callable 'begin' function found for type 'Foo::Container<int>'
error C3312: no callable 'end' function found for type 'Foo::Container<int>'

这是怎么回事?我对 ADL 的解释不正确,或者这是 MSVC 14.0 编译器的错误?

您必须将 beginend 都放入 Foo 命名空间中,ADL 才能工作。这是因为 ADL 将查看相应参数的名称空间以搜索 beginend.

的定义
namespace Foo
{
    template <typename T>
    class Container
    {
    public:

        std::vector<T> values;
    };

    template <typename T>
    typename std::vector<T>::iterator begin(Foo::Container<T>& foo)
    {
        return foo.values.begin();
    }

    template <typename T>
    typename std::vector<T>::iterator end(Foo::Container<T>& foo)
    {
        return foo.values.end();
    }
}

UPD: 不考虑来自全局命名空间的 beginend 的原因是因为更新的标准说 beginend 在关联的命名空间中查找,但不执行普通的非限定查找。这是标准中错误修复的结果 (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1442)。