Vector of std::function with different signatures

前端 未结 7 1748
离开以前
离开以前 2020-12-03 03:32

I have a number of callback functions with different signatures. Ideally, I would like to put these in a vector and call the appropriate one depending on certain conditions.

7条回答
  •  借酒劲吻你
    2020-12-03 04:31

    std::function erases the exact type of the function object but preserves the function call signature. If you cannot bind the extra arguments in advance as Jonathan Wakely recommends, you can use a boost::variant< std::function<...>, std::function<...> > as your vector member. At the call site you can then check if the vector contains the right kind of function object and call it accordingly.

提交回复
热议问题