Imagine I have a type:
struct my_type
{
double operator()(int a)
{
return 3.1415;
}
};
Then I'd like to wrap it in std::function
. Consider two different approaches:
my_type m_t;
std::function<double(int)> f(std::move(m_t));
std::cout << f(4) << std::endl;
Everything works nice as I expected, first digits of PI are printed. Then the second approach:
std::function<double(int)> ff(my_type());
std::cout << ff(4) << std::endl;
It seems to me, that this code is absolutely the same as the first one. rvalue
is passed as an argument for function
wrapper. But the problem is, that the second code doesn't compile! I have really no idea why so.
This is the famous most vexing parse issue. For
std::function<double(int)> ff(my_type());
, you're not declaraing an object of typestd::function<double(int)>
as you expected, but a function namedff
, which returns an object of typestd::function<double(int)>
and has a single (unnamed) parameter which is a pointer to function returning typemy_type
and taking no input.To fix the issue you could add additional parentheses or use braces which was supported from C++11 (braces could be used for disambiguity because it can't be used for parameter list). e.g.
LIVE