2011-06-15 43 views
23

我想爲我的應用程序使用可變參數模板功能,但我不希望按值傳遞對象(因爲對象在我的情況下非常複雜)。我想通過引用傳遞它們(而不是指針)。C++ 0x variadic模板通過引用

void func() 
{ 
} 

template<typename Function1, typename... FurtherFunctions> 
void func(Function1 f1, FurtherFunctions... further_functions) 
{ 
    // doing some processing here... 
} 

如何通過引用傳遞參數並確保它們不被複制?

回答

28

只是說:

template <typename ...Args> 
int myfunction(Args & ... args) 
{ 
    /* ... */ 
} 

同爲const Args & ... argsArgs && ... args

+0

是什麼參數數量與和參數數量之間的差異&&? – usman 2011-06-15 17:19:48

+2

'Args &&'是一個右值引用,您永遠不需要在自己的代碼中編寫它。我只是提到它,因爲它存在,你可能偶爾[看到它](http://stackoverflow.com/questions/6114067/how-to-emulate-c-array-initialization-int-arr-e1-e2- E3行爲/ 6272491#6272491)。 – 2011-06-15 17:22:23

+3

實際上'&&'在這裏是有意義的,因爲你可能會想使用'std :: forward'。 – 2011-06-15 17:34:46