18

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 given some help about the basic syntax in a post below, to bind to functions with explicit template type parameters, but lose the ability to provide template type parameters in the process.

Is it possible to get this to work so that it is still possible to provide template type parameters with future calls?

Cleaned up this code a lot, but it obviously won't compile because I can't find the correct syntax, (are there any ways of doing this)?

Removed the "vector" requirement to simplify this:

Thanks for the help!

#include <functional>
#include <vector>
#include <string>

/***************************************/
template <typename CommandTemplateType>
class Storage
{
  public:
   // No idea how to define this vector to allow Template Parameters
   // static std::vector<std::function<void<ParameterTemplateType>
   //     (std::shared_ptr<ParameterTemplateType>)>> Functions;

   // I really don't need the collection, a single member would kick start my research:
   static std::function<void<ParameterTemplateType>(std::shared_ptr<ParameterTemplateType>)> Function;

  template <typename ParameterTemplateType>
  static void Execute(ParameterTemplateType parameter)
  {
     // Look up index, or loop through all.. 
     // I am trying to invoke the bound function with a template param:
     // Functions[index]<ParameterTemplateType>(parameter);
     // preferably, just:  
     Function<ParameterTempalteType>(parameter); 
  }
};

/***************************************/
template <typename TemplateType>
class MyClass
{

   template <typename ParameterTemplateType>
   void MyFunction(ParameterTemplateType myParameter)
   {
     // Do something; 
   }

   MyClass()
   {
      std::string parameter = L"Test String";

      // Do not know how to include the 
      // template<typename ParameterTemplateType> definition to bind call.
      // Storage::Functions.push_back(
      //     std::bind(&MyClass::MyFunction<ParameterTemplateType>,
//        this, std::placeholders::_1));

     // Or just something like:
     Storage::Function = std::bind(&MyClass::MyFunction<ParameterTemplateType>,
                             this, std::placeholders::_1));

      /***************************************/
      // Call the bound function with an explicit parameter somehow:
      std::string parameter = L"Test String";          
      Storage::Execute<std::string>(parameter);


   }
};
e.s. kohen
  • 213
  • 1
  • 4
  • 21
  • What do you want to be passed as `myParameter`? – Joseph Mansfield Feb 06 '13 at 10:59
  • 5
    Note on terminology: there’s no such thing as a “templated class/function”. There are class templates and function templates. This is quite a fundamental difference: it’s not that something (a “template”, whatever that is) is applied to a class. It’s the other way round: a class template is itself a template – a model, or mould, from which to build a concrete class (by inserting template arguments). Think of a class template as a mould from which to cast a sword (= class), not as a sword with some ornaments on it. – Konrad Rudolph Feb 06 '13 at 11:02
  • If I understand what you're asking correctly, it's impossible. std::bind is a run-time concept, returning (essentially) a function that can be called. Instantiating a function template (filling in the template parameter) must be done at compile time. You can't run-time bind to a function that (may) still need to be compiled... – Chris Hartman Feb 16 '13 at 20:08
  • I figured out how to do it. You have to wrap the templated function in a templated container of some sort. I posted the answer below. Thanks everyone for all of the help! – e.s. kohen Feb 16 '13 at 20:20

4 Answers4

15

The key issues is that in C++11 you cannot do something like:

// Doesn't compile
template <typename TemplateType>
static std::function<void(std::shared_ptr<TemplateType>)> Function;

Classes and Functions can be templated, but not member properties.

The "Magic" is:

/*******************************************************************/
// Define a Function Pointer in a Container
class Storage
{
   template <typename TemplateType>
   struct FunctionContainer {
       static std::function<void(std::shared_ptr<TemplateType>)> Function;
   };
};
/*******************************************************************/
// Initialize FunctionContainer's Static Function Pointer if using static pointer.
template <typename TemplateType>
std::function<void(std::shared_ptr<TemplateType>)> Storage
    ::FunctionContainer<TemplateType>::Function;

You can then Bind a templated function to this function like:

// Bind Function Pointer in Container to a Local Function
class MyClass
{
   template <typename TemplateType>
   void MyFunction(std::shared_ptr<TemplateType> parameter)
   {
     // Do something.
     // You can make this templated or non-templated.
   }
   MyClass()
   {
     // If you really want, you can templatize std::string in the following:
     Storage::FunctionContainer<std::string>::Function 
       = std::bind(&MyFunction<std::string>, this, std::placeholders::_1);
   }
}

And you can invoke all of this and provide a templated type parameter like so:

//Invocation
std::shared_ptr<std::string> parameter;
parameter->get() = "Hello World".
Storage::FunctionContainer<std::string>::Function(parameter);
e.s. kohen
  • 213
  • 1
  • 4
  • 21
  • Never seen such a construct before: ``parameter->get() = "Hello World".`` what does this do? – Gabriel Dec 20 '13 at 10:24
  • Hi! std::shared_ptr<>.get() is just C++ API method that is just a reference to the std:shared_ptr's "pointer" (http://www.cplusplus.com/reference/memory/shared_ptr/) ... Just assigning the value of the string "Hello World", as the value of the shared pointer. I haven't edited my question in a while to clean it up, and it looks like I should do that! Thanks! – e.s. kohen Dec 20 '13 at 14:07
10

The template argument for std::function should be the signature of the function after template type substitution has been done. In your case, neither TemplateType nor FunctionTemplateType have an effect on the signature of the member function MyFunction - it will always return a std::string and take a single std::string argument. Therefore, the std::function you're going to store in your std::vector should be:

static std::vector<std::function<std::string(std::string)>> Functions;

Recall that a member function has an implicit first argument this. You need to bind the first argument of MyClass<...>::MyFunc<...> to the object you want it to be called on. Presumably, since you're binding the function in MyClass's constructor, you want the object to be that MyClass instance. That means your push_back should look like this:

Storage::Functions.push_back(
  std::bind(&MyClass<TemplateType>::MyFunction<int>, this,
    std::placeholders::_1)
);

Now the function that is pushed into Functions is bound to your MyClass object and takes a single argument of type std::string. You can call one of these functions like so:

Storage::Functions[0]("something");
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Thank you! Can I call this from outside the class? Like, if I know the parameter: class Outside { void Do(CustomClass * stuff) { Storage::Functions[find the right signature](stuff);}}; – e.s. kohen Feb 06 '13 at 18:15
  • What if I pushed this function into the vector from within a functor using something like Storage::Functions.push_back(std::bind( this ..... // ) – e.s. kohen Feb 06 '13 at 18:23
  • @WindAndFlame `MyClass::MyFunction` is an instantiation of your function. The `&` ("address of") gives you a *pointer to member function*, similar to a function pointer but the function it points to must be called on a specific object. Member functions are always called on an object unless they are static. – Joseph Mansfield Feb 06 '13 at 21:10
  • sorry, ... is that a keyword? Not seeing how the pointer knows about the parameter type .. – e.s. kohen Feb 06 '13 at 21:12
  • @WindAndFlame Now, what does `bind` do to this function? All of the arguments following the pointer to member function are arguments that should be *bound* to the arguments of that function. Your `MyFunction` has two arguments: the first is the implicit `this` argument that every member function has, and the second is the `std::string`. So we bind the current object `this` to the first argument and, by using a placeholder, we say the other argument (the `std::string` should be left unbound. Now the function that `bind` gives you only takes one argument, the `std::string`. – Joseph Mansfield Feb 06 '13 at 21:13
  • @WindAndFlame `` is the same `` I gave in my answer. Your original question had a `FunctionTemplateType` that had no effect on anything. I was just saying you could give any type for that - `anything` is not something specific. It could be `int`, `Foo`, whatever. – Joseph Mansfield Feb 06 '13 at 21:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24068/discussion-between-sftrabbit-and-wind-and-flame) – Joseph Mansfield Feb 06 '13 at 21:15
  • You mentioned: std::bind(&MyClass::MyFunction, this, std::placeholders::_1) .. What if I want to specify the TemplateType, but want to leave MyFunction still templatable, so that the function can still be called with a template? Functions[0]("MyString"); ? Are there std::placeholders::_? for Template Parameters? – e.s. kohen Feb 07 '13 at 00:34
  • Still trying to figure out how to do something like: Storage::Functions[0]("something"); – e.s. kohen Feb 11 '13 at 23:49
5

If I understood you right... :)

What you want to do is not possible because for template<class T> void foo(T) functions foo<int>() and foo<double> are of different types and you can not create vector holding pointers to both that functions directly because vector is homogeneous container.

To overcome that we can use boost::variant<> to either store pointers to different types of functions or to store arguments of functions.

template<class T> void foo(T);
typedef boost::variant<void (*)(int), void (*)(double)> func_ptr_variant;
std::vector<func_ptr_variant> v;
v.push_back(foo<int>);
v.push_back(foo<double>);

typedef boost::variant<int, double> argument;
std::vector<void (*)(argument)) v;
v.push_back(foo);
v.push_back(bar);
// foo and bar are defined as void foo(argument a) and void bar(argument a)

Unfortunately, in any case you will need to instantiate function templates before inserting them to container because C++ can not do code generation on the fly. I think that it is possible that you know all possible types of arguments that the function may be used with so that may be not a problem.

Manvel
  • 84
  • 1
  • 3
4

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 <functional>
#include <vector>
#include <string>

struct Storage
{
  // Have no idea what this signature should really be:
  static std::vector<std::function<void ()>> Functions;

};
std::vector<std::function<void ()>> Storage::Functions;

template <typename TemplateType>
class MyClass
{
   template <typename FunctionTemplateType>
   std::string MyFunction(std::string myParameter)
   {
     return "Hellö: " + myParameter;

   }
public:
   MyClass()
   {
      Storage::Functions.push_back(
          std::bind( & MyClass<TemplateType>::MyFunction<std::string>, this, "borisbn" )
//                                                       ^^^^^^^^^^^
      );
   }
};

int main() {
    MyClass<int> obj;
}

liveworkspace link

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
borisbn
  • 4,988
  • 25
  • 42