PinkTurtle
PinkTurtle

Reputation: 7041

Delegated factory function call - cannot find the proper syntax

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

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

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

Related Questions