c – 为什么没有范围 – 找到我的重载的开始和结束std :: istream_iterator?

c – 为什么没有范围 – 找到我的重载的开始和结束std :: istream_iterator?,第1张

概述我有这样的代码 std::ifstream file(filename, std::ios_base::in);if(file.good()){ file.imbue(std::locale(std::locale(), new delimeter_tokens())); for(auto& entry : std::istream_iterator<std::string>( 我有这样的代码
std::ifstream file(filename,std::ios_base::in);if(file.good()){    file.imbue(std::locale(std::locale(),new delimeter_tokens()));    for(auto& entry : std::istream_iterator<std::string>(file))    {        std::cout << entry << std::endl;        }}file.close();

其中std :: istream_iterator< std :: string>的begin()和end()
定义如下

template<class T>std::istream_iterator<T> begin(std::istream_iterator<T>& stream){    return stream;}template<class T>std::istream_iterator<T> end(std::istream_iterator<T>& stream){    return std::istream_iterator<T>();}

这是Mark Nelson在dob博士的here中也写过的.唉,代码无法在我的Visual Studio 2012上编译错误消息

error C3312: no callable ‘begin’ function found for type ‘std::istream_iterator<_Ty>’

error C3312: no callable ‘end’ function found for type ‘std::istream_iterator<_Ty>’

问题:有没有什么我没有注意到,编译器中的错误(不太可能,但是为了防止这种情况)或…有什么想法?

根据Xeo的建议,这个问题被大大的清理了.为了提供更多的背景和参考资料,这与我在Stackoverflow上的other question有关,我想知道如何使基于行的解析比通常的循环更清晰.来自互联网的一些编码和检查,我有一个工作草图如下

std::ifstream file(filename,std::ios_base::in);if(file.good()){                   file.imbue(std::locale(std::locale(),new delimeter_tokens()));    for(auto& entry : istream_range<std::string>(file)    {        std::cout << entry << std::endl;        }}file.close();

但是我试图补救的轻微障碍.我认为编写代码中看起来更自然,因为无法编译而不是编译

for(auto& entry : istream_range<std::string>(file)

请记下不同的迭代器. delmet_tokens被定义为像Nawaz一样,已经显示了here(代码不重复)和istream_range,在Code Synthesis博客here中.我认为开始和结束实现应该可以正常工作,正如前面提到的代码综合博客文章

The last rule (the fallback to the free-standing begin()and end() functions) allows us to non-invasively adapt an existing container to the range-based for loop interface.

所以我的问题与所有(ir)相关的背景.

解决方法 如果对本地数组(T foo [N])和成员开始/结束的特殊处理不产生任何结果,则返回依赖于ADL.

§6.5.4[stmt.ranged] p1

otherwise,begin-expr and end-expr are begin(__range) and end(__range),respectively,where begin and end are looked up with argument-dependent lookup (3.4.2). For the purposes of this name lookup,namespace std is an associated namespace.

你的问题是,std :: istream_iterator的关联命名空间(显然是)namespace std,而不是全局命名空间.

§3.4.2[basic.lookup.argdep] p2

For each argument type T in the function call,there is a set of zero or more associated namespaces and a set of zero or more associated classes to be consIDered. The sets of namespaces and classes is determined entirely by the types of the function arguments […].

If T is a fundamental type,its associated sets of namespaces and classes are both empty. If T is a class type (including unions),its associated classes are: the class itself; the class of which it is a member,if any; and its direct and indirect base classes. Its associated namespaces are the namespaces of which its associated classes are members. Furthermore,if T is a class template specialization,its associated namespaces and classes also include: the namespaces and classes associated with the types of the template arguments provIDed for template type parameters […].

注意第二个项目符号的最后(引用)部分.它基本上意味着使用作为全局命名空间的成员的类作为模板参数,使代码工作:

#include <iterator>#include <iostream>template<class T>std::istream_iterator<T> begin(std::istream_iterator<T> is){  return is;}template<class T>std::istream_iterator<T> end(std::istream_iterator<T>){  return std::istream_iterator<T>();}struct foo{};std::istream& operator>>(std::istream& is,foo){  return is;}int main(){  for(foo f : std::istream_iterator<foo>(std::cin))  //                                ^^^  // make global namespace one of the associated namespaces    ;}
总结

以上是内存溢出为你收集整理的c – 为什么没有范围 – 找到我的重载的开始和结束std :: istream_iterator?全部内容,希望文章能够帮你解决c – 为什么没有范围 – 找到我的重载的开始和结束std :: istream_iterator?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1249375.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存