Вопрос

I'm having quite the trouble with using std::result_of, decltype and std::function

with variadic templates .

I have the following function function -

int foo(int a, int b, int c) {
    std::cout << a << b << c << std::endl;
    return 0;
}

And the following class

template <class T, class... Args>
class VariadicTest {
public:
    VariadicTest(const T& func, Args... args) : func(func) {};
private:
    T func;
};

I would like to have a member in the class to save a lambda expression,

for this I need an std::function .

My question is how would I define that std::function properly .

A usecase of this class will look like -

VariadicTest(foo, 1,2,3);

So now I have T = int __cdecl (int, int, int) and Args = (int - 1, int - 2, int - 3)

From this I would like a member function that would look as such:

std::function<std::result_of<T(Args...)::type(Args...)>

now this of course does not compile, nor did 50 or so other stuff I tried .

Basically I need for this example the following declaration

std::function<int(int,int,int)> _f;

And of course for this to be automated per T and Args given .

Это было полезно?

Решение

Try the following:

template <class T, class... Args>
class VariadicTest {
public:
    VariadicTest(const T& func, Args... args) : func(std::bind(func, args...)) {};
private:
    using result_type_t = typename std::result_of<T(Args...)>::type;
    std::function<result_type_t()> func;
};
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top