Explanation of std::function

前端 未结 6 1609
故里飘歌
故里飘歌 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:42

    As far as I understand, std::function turns a function, functor, or lambda into a function object.

    You pretty much summed it up, you can turn any of these into the same thing, an std::function, that you can then store and use as you wish.

    When you are designing a class or an API in general you usually don't have a reason to restrict your features to just one of these, so using std::function gives the liberty of choice to the user of your API, as opposed to forcing users to one specific type. You can even store different forms of these together, it's basically an abstraction of callable types with a given signature and a clearly defined semantic.

提交回复
热议问题