[英]Is the std::istream type EqualityComparable?
My question would have a boolean answer: yes or not. 我的问题会有一个布尔答案:是或否。 Whichever it would be, can someone explain how the following code is compiled by both GNU-g++ 4.9.2 and clang 3.5, while GNU-g++ 5.1.1 no longer accepts it, claiming that there is no matching
operator==
? 无论哪个,有人可以解释GNU-g ++ 4.9.2和clang 3.5如何编译以下代码,而GNU-g ++ 5.1.1不再接受它,声称没有匹配的
operator==
?
And how it could be changed, for this last compiler, in order to have the same results ie to have the operator>>
able to distinguish, in such a simple way, whether it is called by the standard input stream or by something else? 对于最后一个编译器,为了获得相同的结果,即让
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....
No. There's no operator==
that operates on std::istream
objects. 没有。在
std::istream
对象上没有operator==
。
Your being able to compare two std::istream
s is an unfortunate consequence caused by the conversion function of std::istream
, specifically operator void*
. 你能够比较两个
std::istream
s是由std::istream
的转换函数引起的一个不幸后果,特别是operator void*
。 In the expression i == std::cin
, both i
and std::cin
are implicitly converted to void*
, and the resulting values are compared. 在表达式
i == std::cin
, i
和std::cin
都隐式转换为void*
,并比较结果值。 This is not really meaningful. 这不是很有意义。 This conversion function was removed in C++11 (replaced by an
explicit
conversion function, which won't be called in this situation), so if you enable the C++11 mode, the code will not compile. 此转换函数已在C ++ 11中删除(由
explicit
转换函数替换,在这种情况下不会调用),因此如果启用C ++ 11模式,代码将无法编译。
As is stated here , if you want to check whether the reference i
refers to std::cin
, you can use &i == &std::cin
. 正如指出这里 ,如果你想检查是否参考
i
指std::cin
,您可以用&i == &std::cin
。
Standard C++ streams don't have ==
, >
, <
operators because they are not very meaningful in that context: what should "stream a is > than stream b" mean? 标准C ++流没有
==
, >
, <
运算符,因为它们在该上下文中不是很有意义:“流a是>而不是流b”是什么意思?
However, it shouldn't matter what type of istream
you're dealing with: as long as you're not using specific methods defined in descendant classes (like is_open
), base methods are shared (extracting, for instance). 但是,你要处理的是什么类型的
istream
并不重要:只要你没有使用后代类中定义的特定方法(比如is_open
),就可以共享基本方法(例如,提取)。 Whether you're extracting a string
from a istringstream
or a ifstream
, you just do in >> str
and let polymorphism take action. 无论你是从
istringstream
还是ifstream
提取string
,你只需要in >> str
并让多态性采取行动。
If you really want to know the polymorphic type of the istream
, you can use typeid
or, simply, function overloading. 如果您真的想知道
istream
的多态类型,可以使用typeid
或简单地使用函数重载。 In your case, with RTTI( typeid
) 在您的情况下,使用RTTI(
typeid
)
if ( typeid(in) == typeid(std::cin) )
// cin
else
// another istream type
Using overloading: 使用重载:
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.