我試圖模仿的std :: thread構造函數的功能:如何可變參數模板PARAMS綁定功能
template< class Function, class... Args >
explicit thread(Function&& f, Args&&... args);
我試着調試器步進,看看它是如何工作,但我想不出它出。
如何創建和存儲線程的構造函數的綁定類型呢?
像這樣的東西(語法錯誤的可能):
使用示例:
int test(int i) {return i;}
int main(){
myClass my(test, 5);
my.evaluate();
}
請注意,我不關心,如果somehowBind
功能將忽略返回類型,即其返回類型可以像std :: function一樣。 我想要做的就是了解如何將class... Args
綁定到給定函數f
,以便在調用somehowBind
之後,它將像std :: bind那樣操作。 爲了澄清我的觀點,你可以想想我想實現如下:
thread t(test, 5); // unlike the usual std:::thread, this one is created in suspended mode therefore I need somehow to bind `f` with `5` and store it
t.start(); // now t is executed
它還挺提醒C#和Java線程,他們不能施工後立即執行。
我認爲你需要解釋一下更正是你想要達到的目標。 – tumdum
在包裝代碼的各個層次深處,會有一個'std :: tuple'存儲可變參數,指針或對它們的引用。正如Tomasz所說,給出一些關於你的包裝代碼應該是什麼樣的細節。 – bobah
你在尋找'std :: function'嗎? 'class myClass {std :: function m; public:template myClass(F && f,Args && ... args):m(std :: bind(std :: forward (f),std :: forward (args)。 ();} {} void evaluate(){m();}};' –
dyp