考虑下面的程序:(A,B)
#include<functional>
typedef std::function< int(int) > F;
F operator+(F, F)
{
return F();
}
int f(int x) { return x; }
int main()
{
operator+(f,f); // ok
f+f; // error: invalid operands to binary expression
}
为什么最后一行f+f;
不能编译?为什么与operator+(f,f);
不一样?对此标准的参考将不胜感激。
可能的重复http://stackoverflow.com/questions/13869150/rules-for-lookup-of-operators-in-c11? – Cubbi
@Cubbi我不认为这是重复的,它是关于*重载*成员和块作用域操作符。 –
那么这个答案提出了查找规则(对于'a + b'和'operator +(a,b)'来说这是相当不同的),所以它会回答你的问题的标题。但是,在这种情况下,标准中有一个捷径。 – Cubbi