template argument deduction/substitution failed, when using std::function and std::bind

后端 未结 2 364
傲寒
傲寒 2020-12-30 21:32

I have a compile error when using std::function in a templated member function, the following code is a simple example:

#include 
#include          


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 22:06

    You can make type deduction work with some variant of:

    template
    void setCallback(CALLBACK cb) {
      typedef CALLBACK::first_argument_type T;
      static_assert(is_same_type>::value);
      ...
    }
    

    This way CALLBACK can be determined by looking at the argument. It might get into trouble if bind doesn't actually return a std::function but rather something that can be cast as one. I'm not sure.

提交回复
热议问题