c++ - C++ 在初始化 std::function 时,我们如何将占位符绑定到引用/引用参数?
问题描述
#include <functional>
#include <string>
int fn( int a, int & b ) { return a+b; }
struct Fn_struct {
std::string name {};
// std::function<int (int,int&)> my_fn {};
std::function<decltype(fn)> my_fn {};
};
int main()
{
Fn_struct my_fn_struct1 {"fn(a,b)", std::function<decltype (fn)> {fn} };
Fn_struct my_fn_struct2 {"fn(a,b)", {fn} };
Fn_struct my_fn_struct3 {"fn(a,b)", {std::bind( fn, 1, 2) }};
auto fn_b = std::bind( fn, 1, std::placeholders::_1 );
Fn_struct my_fn_struct4 {"fn(a,b)", {std::bind( fn, 1, std::placeholders::_1) }}; // todo: why can't int b be a reference?
}
my_fn_struct4 无法编译,因为找不到绑定的构造函数。但是,如果 b 不是参考,它会编译。
另一方面 fn_b 确实编译。
任何解释将不胜感激。
请不要问我为什么要这样做。除非完全必要,否则我宁愿不使用指针来完成此任务。
解决方案
std::bind( fn, 1, std::placeholders::_1 )
返回一个可转换为的对象,std::function<int(int &)> my_fn{};
因为传递了一个带有 2 个参数的函数并且第一个参数绑定为 1:
#include <functional>
#include <string>
int fn( int a, int & b ) { return a+b; }
struct Fn_struct {
std::string name {};
std::function<int(int &)> my_fn{};
};
int main()
{
Fn_struct my_fn_struct4 {"fn(a,b)", {std::bind( fn, 1, std::placeholders::_1) }};
}
线
Fn_struct my_fn_struct3 {"fn(a,b)", {std::bind( fn, 1, 2) }};
工作,因为
如果在调用 g() 时提供的某些参数与存储在 g 中的任何占位符都不匹配,则评估并丢弃未使用的参数。
推荐阅读
- javascript - 如何在 TypeScript 中使用 document.getElementById() 方法?
- rx-java2 - 为什么doOnComplete会立即执行而不管延迟()
- windows - 在 Windows 上链接 SHARED 库
- spring-boot - AWS Lambda - Spring Boot 未处理请求
- visual-studio - Visual Studio 在括号内快速键入字符串
- compilation - 在 AWS Lambda 上将 PDF 转换为 JPEG
- xamarin - 如何在 XAMARIN android 应用程序中向 HockeyApp SDK 发送已处理的异常?
- firebase - 如何正确跟踪游戏战斗结果游戏分析事件
- java - 为 HANA JDBC 创建一个连接字符串?
- ballerina - 如何确保我的类型和函数文档出现在 Ballerina Central 中?