【发布时间】:2012-09-26 09:32:44
【问题描述】:
我怀疑在下面的代码中,由于最棘手的解析问题,使用构造函数参数和函数调用运算符参数调用临时函数对象在某种程度上是模棱两可的。
#include <iostream>
class Fun
{
public:
explicit Fun(int i): v_(i) {}
void operator()(int j) const
{
std::cout << (v_ + j) << "\n";
}
private:
int v_;
};
int main()
{
int a = 1;
int b = 2;
Fun(a)(b); // ERROR: conflicting declaration 'Fun a'
(Fun(a))(b); // OK: prints 3
return 0;
}
Ideone 上的输出。
添加像(Fun(a))(b) 这样的括号可以解决问题,但我不太明白Fun(a)(b) 是如何被解析为声明Fun a。
【问题讨论】:
标签: c++ constructor arguments most-vexing-parse function-object