简体   繁体   English

C ++ 11-获取result_of,decltype,std :: function和可变参数模板一起工作

[英]c++11 - getting result_of, decltype, std::function and variadic templates working together

I'm having quite the trouble with using std::result_of, decltype and std::function 我在使用std :: result_of,decltype和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, 我想在班上有一个成员来保存lambda表达式,

for this I need an std::function . 为此,我需要一个std :: function。

My question is how would I define that std::function properly . 我的问题是如何正确定义std :: function。

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) 所以现在我有T = int __cdecl(int,int,int)和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 . 现在,这当然不会编译,我也没有尝试50多个其他东西。

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 . 当然,这要根据给定的T和Args自动进行。

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM