Reputation: 70516
I suspect that in the code below, a call to a temporary function object with both a constructor argument and a function call operator argument is somehow ambiguous due to a most-vexing-parse issue.
#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;
}
Output on Ideone.
Adding parentheses like (Fun(a))(b)
fixes the problem, but I can't quite understand how Fun(a)(b)
can be parsed as a declaration Fun a
.
Upvotes: 1
Views: 248
Reputation: 3645
Simple example to understand function declaration syntax:
#include <iostream>
void (foo)(int a) {
std::cout << a << std::endl;
}
int main() {
foo(5);
}
So in your case you have to use this:
Fun(a).operator()(b);
(Fun(a))(b);
Upvotes: 1
Reputation: 92241
Unfortunately Fun(a)(b);
can be parsed as Fun a(b);
which is a declaration, and not a temporary object.
Upvotes: 2