Can I use (boost) bind with a function template?

后端 未结 2 500
花落未央
花落未央 2020-12-06 17:24

Is it possible to bind arguments to a function template with (boost) bind?

// Define a template function (just a silly example)
template

        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 17:57

    I don't think so, only because boost::bind in this case is looking for a function pointer, not a function template. When you pass in FCall2Templ, the compiler instantiates the function and it is passed as a function pointer.

    However, you can do the following using a functor

    struct FCall3Templ {
    
      template
      ARG1 operator()(ARG1 arg1, ARG2 arg2) {
        return arg1+arg2;
      }
    };
    int main() {
      boost::bind(FCall3Templ(), 45, 56)();
      boost::bind(FCall3Templ(), 45.0, 56.0)();
      return 0;
    }
    

    You have to specify the return type, since the return type is tied to the inputs. If the return doesn't vary, then you can just add typedef T result_type to the template, so that bind can determine what the result is

提交回复
热议问题