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

我有这样的代码
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&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...