Why does std :: function not work in this situation?

Suppose I have a type:

struct my_type
{
    double operator()(int a)
    {
        return 3.1415;
    }
};

Then I would 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 well, as I expected, the 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 absolutely matches the first. rvaluepassed as an argument to the wrapper function. But the problem is that the second code does not compile! I really don’t know why.

+4
source share
1 answer

. std::function<double(int)> ff(my_type()); std::function<double(int)>, , ff, std::function<double(int)> () , my_type .

, , ++ 11 ( , ). .

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

+10

Source: https://habr.com/ru/post/1665080/


All Articles