c ++将各种参数传递给父类构造函数(thread c ++ 11)

时间:2014-09-05 23:18:45

标签: c++ multithreading c++11

谁知道如何将参数从继承类传递给基本类构造函数,它接受不同数量的参数?我的意思是......“thread”类有一个带有不同数量参数的初始化构造函数,它表示为:

template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args);

所以...我想创建一个具有相同初始化构造函数的继承类,并且新类中的构造函数必须将所有参数传递给基本(线程)类,但不知道如何=)

例如(我想要的):

class my_thread : public std::thread {
private:
    .....
public:
    my_thread( WHAT TO WRITE HERE? ) : thread (AND HERE ) {};
    .....
};

void my_func(int arg_1, char arg_2) {
    .....
}

int main() {
    my_thread mt(my_func, 20, -6);
    mt.join();
}

任何解决方案?

1 个答案:

答案 0 :(得分:2)

template <class Fn, class... Args,
          class = typename std::enable_if<
                  !std::is_same<typename std::decay<Fn>::type,
                                my_thread>::value>::type>
explicit my_thread (Fn&& fn, Args&&... args) 
       : thread(std::forward<Fn>(fn), std::forward<Args>(args)...) { }

关于从标准库类型继承的常见警告适用。如果您传递enable_ifmy_thread会从重载解析中删除此构造函数,实质上重复LWG 2097的解析。

相关问题