C++ How to Reference Templated Functions using std::bind / std::function

前端 未结 4 1963
小鲜肉
小鲜肉 2020-12-28 17:30

If you have a templated class or a templated function, (or combination of the two), how do you bind that function, (preserving the template type parameter)?

I was gi

4条回答
  •  执念已碎
    2020-12-28 17:49

    MyClass's c-tor doesn't know anything about FunctionTemplateType that's why it can push_back only explicit specialized (sorry, it's term of mine... I don't know the right term) like this

    #include 
    #include 
    #include 
    
    struct Storage
    {
      // Have no idea what this signature should really be:
      static std::vector> Functions;
    
    };
    std::vector> Storage::Functions;
    
    template 
    class MyClass
    {
       template 
       std::string MyFunction(std::string myParameter)
       {
         return "Hellö: " + myParameter;
    
       }
    public:
       MyClass()
       {
          Storage::Functions.push_back(
              std::bind( & MyClass::MyFunction, this, "borisbn" )
    //                                                       ^^^^^^^^^^^
          );
       }
    };
    
    int main() {
        MyClass obj;
    }
    

    liveworkspace link

提交回复
热议问题