我需要在回圈中重復將引數包傳遞給函式,如下所示:
void printString(string str)
{
cout << "The string is: \"" << str << "\"" << endl;
}
template <typename FunctionType, typename ...Args>
void RunWithArgs(FunctionType functionToCall, Args...args)
{
for (int i = 0; i < 3; i )
functionToCall(forward <Args>(args)...);
}
int main()
{
string arg("something");
RunWithArgs (printString, arg);
return 0;
}
編譯器給了我一個提示,我正在使用一個移動的物件,果然,字串只在第一次傳遞:
The string is: "something"
The string is: ""
The string is: ""
如何在回圈的每次迭代中將引數包傳遞給函式?
uj5u.com熱心網友回復:
在這一行:
functionToCall(forward <Args>(args)...);
您正在轉發引數,在這種情況下,它會移動它。如果您不想移動,而是想復制,請執行以下操作:
functionToCall(args...);
這是一個活生生的例子。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/427996.html
上一篇:朋友注射會不會畸形?
