Range-based for loops and ADL
使用Visual Studio 2015,我无法使用Argument Dependent Lookup(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编译器的错误?
解决方法
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:不考虑全局命名空间的开始和结束的原因是因为更新的标准说在关联的命名空间中查找了开始和结束,但是没有执行普通的不合格的查找.这是标准中错误修复的结果(http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1442).
原文地址:https://www.jb51.cc/c/110668.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。