繁体   English   中英

std :: istream类型是EqualityComparable吗?

[英]Is the std::istream type EqualityComparable?

我的问题会有一个布尔答案:是或否。 无论哪个,有人可以解释GNU-g ++ 4.9.2和clang 3.5如何编译以下代码,而GNU-g ++ 5.1.1不再接受它,声称没有匹配的operator==

对于最后一个编译器,为了获得相同的结果,即让operator>>能够以这种简单的方式区分它是由标准输入流还是由其他东西调用?

  # include <iostream>
  # include <fstream>

  struct S {};

  std::istream& operator >> (std::istream& i, S& s)
   {
    if(i == std::cin) std::clog << "this is standard input\n";
    else std::clog << "this is some other input stream\n";

    return i;
    }

  int main(int narg, const char ** args)
   {
    S s;
    std :: cin >> s;
    std::ifstream inp(args[1]);
    inp >> s;
    }
  // to be executed with the name of an existing
  // disk file on the command line....

没有。在std::istream对象上没有operator==

你能够比较两个std::istream s是由std::istream的转换函数引起的一个不幸后果,特别是operator void* 在表达式i == std::cinistd::cin都隐式转换为void* ,并比较结果值。 这不是很有意义。 此转换函数已在C ++ 11中删除(由explicit转换函数替换,在这种情况下不会调用),因此如果启用C ++ 11模式,代码将无法编译。

正如指出这里 ,如果你想检查是否参考istd::cin ,您可以用&i == &std::cin

标准C ++流没有==><运算符,因为它们在该上下文中不是很有意义:“流a是>而不是流b”是什么意思?

但是,你要处理的是什么类型的istream并不重要:只要你没有使用后代类中定义的特定方法(比如is_open ),就可以共享基本方法(例如,提取)。 无论你是从istringstream还是ifstream提取string ,你只需要in >> str并让多态性采取行动。

如果您真的想知道istream的多态类型,可以使用typeid或简单地使用函数重载。 在您的情况下,使用RTTI( typeid

if ( typeid(in) == typeid(std::cin) )
    // cin
else
   // another istream type

使用重载:

std::istream& operator >> (std::istream& i, S& s)
{
     std::clog << "this is standard input\n";   
     return i;
}

std::ifstream& operator>>(std::ifstream& i, S& s)
{
     std::clog << "this is some file input stream\n";
     return i;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM