我有一个ThreadPool
类的enqueue
功能:C++模板 - 可变参数模板和路过常引用
class ThreadPool
{
public:
//(Code removed here)
template <typename ... Args, typename Fun>
JobId enqueue(Fun func, Args ... args);
//(Code removed here)
}
我用它在这些非静态成员函数loadStuff
上Object
类:
class Object
{
//(Code removed here)
void init(const PrepareData & prepareData);
virtual bool loadStuff(const PrepareData & prepareData);
//(Code removed here)
}
通过调用QObject :: init:
void QObject::init(const PrepareData &prepareData)
{
threadPool->enqueue(&loadStuff, this, prepareData);
}
但我注意到prepareData是通过复制传递的,这会消耗大量的内存并显着减慢程序的速度(并且是无用的)。
所以我删除了PrepareData中的copy ctor和赋值操作符。该程序不再编译,因为可变参数模板通过值而不是参考来获取参数。
因此,我宣布排队按引用传递的可变参数模板参数:
template <typename ... Args, typename Fun>
JobId enqueue(Fun func, Args&... args);
现在拷贝构造函数不再被调用,但我得到以下错误:
object.cpp:21: error: no matching function for call to
'ThreadPool::enqueue(bool (Object::*)(const PrepareData&), Object *, const PrepareData&)' threadPool->enqueue(&prepareType, this, loadStuff);
所以我如何做到这一点,我相当遗憾。我可以,而不是通过const PrepareData &
,通过const PrepareData *
副本,但我想了解为什么它不适用于const引用。
说'参数数量&& ...'。 –