c++ - 将函数作为参数传递的良好做法 : copy, 引用,const 引用?

标签 c++ c++11 functor lambda

<分区>

Possible Duplicate:
template pass by value or const reference or…?

以下对于将函数作为参数的函数的最佳做法是什么:

template<class Function> void test1(Function f);
template<class Function> void test2(Function& f);
template<class Function> void test3(const Function& f);

其中传递的函数可以是仿函数、std::function、函数指针或 lambda 函数。

最佳答案

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

template<class Function> void test(Function&& f);

关于c++ - 将函数作为参数传递的良好做法 : copy, 引用,const 引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13483315/

相关文章:

c++ - 在 C++ 代码库中使用 Ragel 解析字符串的原因是什么?

C++ - 模板嵌套类的引用初始化无效

c++ - 如何为范围分配器模型启用自定义容器

java - 是否可以在 Java 8 中使用对象的特定方法(或静态方法)作为仿函数

haskell - 在 haskell 中如何确定一个类型是否为仿函数?

haskell - 具有类型约束的 GADT 的仿函数实例

c++ - 代码c++ VS 2010中的LPCWSTR到字符串转换问题

c++ - 为什么返回子对象时 "that is the complete object of a subobject"对象的生命周期延长了?

c++11 - 避免 std::shuffle 中的自分配

c++ - 当 float 转换为 int 时,此代码中如何/为什么会发生缩小