Reputation: 7041
I would like to pass a factory function pointer and variadic length arguments to a function but can't find the proper syntax. Something like this:
#include <memory>
struct Ressource {
int a;
float b;
static std::shared_ptr<Ressource> make(int _a, float _b) {
return std::shared_ptr<Ressource>(new Ressource(_a, _b));
}
private:
Ressource(int _a, float _b) : a{ _a }, b{ _b } {}
};
template<typename R, typename... Args>
void delegate_(std::shared_ptr<R>(R::*factory)(Args...), Args... args) {
std::shared_ptr<R> ressource = factory(args...);
}
void test() {
delegate_(&Ressource::make, 1, 3.0f);
}
I get (msvc) : error C2064: [error follows in french :]
Thank you.
Upvotes: 0
Views: 31
Reputation: 32732
The factory method make
should be a static member of Resource
. Then delegate_
would take just a pointer to a function, rather than a pointer to a member function, and it would then be able to call the factory function. The problem with your current implementation is that in order to call factory
from within delegate_
, you need to provide a Resource
object to call it on.
Upvotes: 2