将 C++11 std::function 传递给采用 boost::function 的遗留函数是否安全?
Is it safe to pass C++11 std::function to a legacy function that takes boost::function
我们有一个大量使用 boost::function 的遗留系统,现在决定转向更新的现代C++标准。假设我们有一个这样的遗留函数:
void someFunction(boost::function<void(int)>);
直接传入 C++11 函数是否安全?
//calling site, new C++11 code
std::function<void(int)> func = [](int x){
//some implementation
}
someFunction(func); //will this always work?
boost::函数是否也能优雅地处理标准 C++11 lambda?
// will this also work?
someFunction([](int x)->void{
//some implementation
});
是的,这将起作用。
重要的是,您不应该将类型安全与兼容性混淆。你不是把std::function
当作boost::function
传递。您告诉编译器将std::function
包装到boost::function
中。
这可能效率不高 - 因为每个都会在调用时添加另一层间接寻址。但它会起作用。
lambdas 也是如此:lambda 没有什么神奇之处,它只是函数对象的语法糖。
相关文章:
- "error: no matching function for call to"构造函数错误
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 将函数包装器转换为 std::function
- 类型擦除的std::function与虚拟函数调用的开销
- 如何为 v8::P ersistent<v8::Function> 创建到 c++ 函数的包装器
- C++复制函数重载导致"must be a nonstatic member function"错误
- C++派生类重载函数(带有 std::function 参数)不可见
- 如何将函数指针从 std::function 传递到 Linux 克隆?
- 如何在跳过所有内部实现的同时跳转到 GDB 中 std::function 中的函数?
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- C++:从重载函数转换为 std::function
- 仅当一个参数中未使用 std::function 时,模板函数替换才有效
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- 插件:TypeError:addons.function() 不是一个函数
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 将另一个类的成员函数传递到 std::function 参数中
- 如何从类成员函数返回指针,例如 size_t * class :: function(); 并使用类析构函数 ~size
- 创建函数变体向量时"No matching function for call"错误
- "attempting to reference a deleted function"函数参数
- std::function -> 函数指针