【发布时间】:2015-11-16 13:42:25
【问题描述】:
我的问题会有一个布尔答案:是或否。不管是哪一种,谁能解释一下下面的代码是如何被 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....
【问题讨论】:
-
我不明白流之间的相等可能意味着什么。身份,当然,但你有
&。 -
@LightnessRacesinOrbit:一个合理(但完全假设)的例子是两个
fstreams 比较相等,如果他们访问同一个文件。 -
@MSalters:我认为这没有意义。您只是在比较一个复杂得多的实体的一个属性。缓冲区呢?旗帜?输入/输出模式?光标?
标签: c++ c++11 equality istream