Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何调用boost::thread的可变模板版本的ctor?_C++_Boost - Fatal编程技术网

C++ 如何调用boost::thread的可变模板版本的ctor?

C++ 如何调用boost::thread的可变模板版本的ctor?,c++,boost,C++,Boost,下面是未能通过编译命令g++-std=c++11 a.cpp-Wall-lboost\u thread-lboost\u system的代码,如果回调参数的数量超过9,在本例中,是在定义了TRY\u VARIADIC时 #include <iostream> #include <boost/thread.hpp> void foo(void) {} template <typename T, typename... argTs> void foo(T a0

下面是未能通过编译命令
g++-std=c++11 a.cpp-Wall-lboost\u thread-lboost\u system
的代码,如果回调参数的数量超过9,在本例中,是在定义了
TRY\u VARIADIC

#include <iostream>
#include <boost/thread.hpp>

void foo(void) {}

template <typename T, typename... argTs>
void foo(T a0, argTs ...args)
{
    std::cout << a0 << std::endl;
    foo(args...);
}

int main(int argc, char *argv[])
{
#if TRY_VARIADIC
    boost::thread t(foo<int, int, int, int, int, int, int, int, int, int>, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
#else
    boost::thread t(foo<int, int, int, int, int, int, int, int, int>, 1, 2, 3, 4, 5, 6, 7, 8, 9);
#endif
    t.join();
    return 0;
}
#包括
#包括
void foo(void){}
样板
无效foo(T a0,argTs…args)
{

std::cout我认为这是不可能的。根据线程:

带参数的线程构造函数

 template <class F,class A1,class A2,...>
 thread(F f,A1 a1,A2 a2,...);
模板
螺纹(F、A1、A2、…);

注意

目前,除了函数
f
之外,最多可以指定九个附加参数
a1
a9

您可以将它们全部包装在
std::tuple
中。或者,由于您使用的是支持可变模板的编译器,所以您可以使用
std::thread

boost::thread( [=]{ f(1,2,3,4,5,6,7,8,9,10); } );

boost::thread
std::thread
使用参数调用的功能很可爱,但lambda大多会使其过时。如果您有仅移动数据,您确实需要C++14,或显式传递数据,但这是一个极端情况。

如果涉及boost 1.58.0,在boost/thread/detail/thread.hpp中,有两个未记录的变量adic ctor模板。其中一个模板的第一个参数是线程属性。因此可以
线程t(attr、callback、arg0、arg1)
。这个可变的ctor模板实际上就是我想要使用的,它具有与上面示例中提到的ctor相同的常量宏来启用/禁用它们的可用性。@Cody在我看来,这项功能并没有完成(新模板仍然有限)@Cody你为什么要使用它?它是隐藏的并且没有文档记录,这表明这可能不是一个好主意。谢谢,但我仍然想知道如何使用boost提供的可变因子模板,其定义在boost/thread/detail/thread.hpp(377)中。顺便说一句,funtor也可以在这种情况下用于传递变量作为functor的ctor的参数。“如果您需要9个以上的参数,可能会遗漏一些”-源未知