Explanation of std::function

前端 未结 6 1594
故里飘歌
故里飘歌 2020-12-31 09:58

What is the purpose of std::function? As far as I understand, std::function turns a function, functor, or lambda into a function object.

I

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 10:46

    Consider a simple use case:

    /* Unspecified */ f = [](int x, int y){ return x + y; };
    f = [](int x, int y){ return x - y; };
    int a = 42;
    f = [&a](int x, int y){ return a * x * y; };
    

    How would you specify /* Unspecified */?

    Furthermore,

    std::queue jobs;
    jobs.push_back([]{ std::cout << "Hi!\n"; });
    jobs.push_back([]{ std::cout << "Bye!\n"; });
    for(auto const &j: jobs) j();
    

    What value_type should be kept in jobs?

    Finally,

    myButton.onClick(f);
    

    What type does f have? A template parameter? Okay, but how is it registered internally?

提交回复
热议问题