为什么在这种情况下std::bind中需要占位符
Why are placeholders required in std::bind in this case?
在回答这个问题时,我意外地看到了以下事实。
请看这个例子:
void func1(const char *str1, const char *str2) { puts(str1); puts(str2); }
...
auto fn = std::bind(func1, "asdf");
fn("1234");
编译失败:
prog.cpp: In function ‘int main()’:
prog.cpp:11:14: error: no match for call to ‘(std::_Bind<void (*(const char*))(const char*, const char*)>) (const char [5])’
fn("1234");
^
如果我把代码改成这样,它会很好地工作:
auto fn = std::bind(func1, "asdf", _1);
输出为:
asdf
1234
为什么I仅绑定第一个参数。。std::bind
是否不可能自动对其他参数执行占位符操作?(我希望std::bind1st
在C++98中也能得到同样的结果。)为什么??
通常在使用bind
时,这是不可能的,因为func1
可能有默认参数,或者重载了不同数量的参数,或者可能是一个函数,其operator()
是一个采用参数包的函数模板。
在这种情况下,有许多不同的方法来调用func1
。我认为bind
不希望选择一个并用占位符填空。
在您的示例中,它是明确的,但我不确定确定确定明确的情况是什么,用代码准确地检测它们,并在标准中准确地定义它们会有多容易。
bind1st
在设计上是明确的,因为它专门用于绑定2-参数函子的第一个参数。
相关文章:
- 将模板与类模板扣除占位符参数匹配
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- Tensorflow C++ 占位符初始化
- std::bind 不会接受 bind 占位符的 std::cref - 为什么?
- qt 中的占位符
- 我可以在 c++ 中使用占位符作为数组的索引吗?
- 为什么协程不允许使用占位符返回类型?
- TensorFlow类占位符以及其他定义输入张量的方法
- QtLineEdit 占位符:如何让用户在插入模式下将字符 1 替换为 1
- 非类型模板参数中的占位符类型是否涉及作为模板参数传递的函数的重载解析?
- 是否可以使用自动占位符来推断非类型模板参数中的函数结果
- 在另一个函数中使用std::bind返回对象和占位符作为参数
- std::bind 占位符的重载运算符
- 如何在视觉C++中与 4 个以上的占位符进行 std::bind
- boost::bind() 一个成员函数,用于提升::函数,对象指针作为占位符
- 在以下示例中,占位符在 Boost::bind 中的角色
- 为什么 std::bind 的占位符不使用非类型模板参数实现?
- 省略std::bind中的std::占位符
- 为什么在这种情况下std::bind中需要占位符
- 为什么在这个例子(成员函数)中没有占位符std::bind不能工作?