Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't std::function work in this situation?

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.

like image 592
Aleksandr Tukallo Avatar asked Dec 23 '22 22:12

Aleksandr Tukallo


1 Answers

This is the famous most vexing parse issue. For std::function<double(int)> ff(my_type());, you're not declaraing an object of type std::function<double(int)> as you expected, but a function named ff, which returns an object of type std::function<double(int)> and has a single (unnamed) parameter which is a pointer to function returning type my_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.

std::function<double(int)> ff1((my_type())); 
std::function<double(int)> ff2(my_type{}); 
std::function<double(int)> ff3{my_type()}; 
std::function<double(int)> ff4{my_type{}}; 

LIVE

like image 139
songyuanyao Avatar answered Jan 06 '23 04:01

songyuanyao