将函数作为参数传递的好习惯:复制,引用,const引用?

Vin*_*ent 3 c++ lambda functor c++11

可能重复:
模板传递值或const引用或...?

对于将函数作为参数的函数,下面的优良做法是什么:

template<class Function> void test1(Function f);
template<class Function> void test2(Function& f);
template<class Function> void test3(const Function& f);
Run Code Online (Sandbox Code Playgroud)

其中传递的函数可以是仿函数,std ::函数,函数指针或lambda函数.

eca*_*mur 6

使用通用引用,您不需要考虑它:

template<class Function> void test(Function&& f);
Run Code Online (Sandbox Code Playgroud)