【发布时间】:2011-12-13 11:59:01
【问题描述】:
#include <functional>
#include <iostream>
struct A {
friend bool operator==( const A & a, const A & b ){
return true;
}
};
namespace {
bool operator!=( const A &a, const A & b){
return !(a==b);
}
}
int main(int argc, char **argv) {
std::not_equal_to<A> neq;
A a;
bool test = neq(a, a);
return test ? 0 : 1;
}
这在CC(SunOs 编译器)上失败:
Error: The operation "const A != const A" is illegal.
"tempcc.cpp", line 16: Where: While instantiating "std::not_equal_to<A>::operator()(const A&, const A&) const".
"tempcc.cpp", line 16: Where: Instantiated from non-template code.
在g++ 上使用:
/usr/local/include/c++/3.3.2/bits/stl_function.h: In member function `bool std::not_equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = A]':
tempcc.cpp:16: instantiated from here
/usr/local/include/c++/3.3.2/bits/stl_function.h:183: error: no match for 'operator!=' in '__x != __y'
但是,如果我删除 #include <iostream> 行,它将编译并运行得很好。有人敢解释吗?
【问题讨论】:
-
在 IdeOne 上根本不起作用,即使删除了
#include <iostream>。 -
以上都来自SunOS。我也在 AIX 上使用 g++ 进行了尝试,结果是相同的(使用 iostream 它不会编译,没有它会编译)。如果没有其他人在我之前尝试过,我将尝试在 Linux 上运行它以查看它的作用。
-
@Seth 可能是。但随后删除“命名空间{}”也解决了这个问题。而且我认为如果是您链接的情况,则不应该这样做。我必须考虑更多。
-
以前试过。不会改变任何事情。但似乎它们是密切相关的问题。
标签: c++ functional-programming g++ iostream sunos