Using bind1st for a method that takes argument by reference(将 bind1st 用于通过引用获取参数的方法)
问题描述
我有一个这样的结构:
struct A {
void i(int i) {}
void s(string const &s) {}
};
现在当我尝试这个时:
bind1st(mem_fun(&A::i), &a)(0);
bind1st(mem_fun(&A::s), &a)("");
第一行编译正常,但第二行报错:
The first line compiles OK, but the second generates an error:
c:program files (x86)microsoft visual studio 10.0vcincludexfunctional(299): error C2535: 'void std::binder1st<_Fn2>::operator ()(const std::basic_string<_Elem,_Traits,_Ax> &) const' : member function already defined or declared
with
[
_Fn2=std::mem_fun1_t<void,A,const std::string &>,
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:program files (x86)microsoft visual studio 10.0vcincludexfunctional(293) : see declaration of 'std::binder1st<_Fn2>::operator ()'
with
[
_Fn2=std::mem_fun1_t<void,A,const std::string &>
]
c:worksourcesexception estexceptionmain.cpp(33) : see reference to class template instantiation 'std::binder1st<_Fn2>' being compiled
with
[
_Fn2=std::mem_fun1_t<void,A,const std::string &>
]
可能是什么问题?我该如何解决?
What could be the problem? How could I fix it?
似乎任何引用参数都有问题.因此,如果我将 i 方法更改为 void i(int &i) {},我会收到类似的错误.
It seems that any reference argument is a problem. So if I change the i method to void i(int &i) {} I get a similar error.
推荐答案
std::bind1st 和 std::bind2nd 不接受接受引用参数的函子,因为它们本身形成对这些参数的引用.你可以
std::bind1st and std::bind2nd don't accept functors which take reference arguments, because they themselves form references to these arguments. You can
- 为函数输入使用指针而不是引用
- 使用 boost::bind
- 接受复制字符串的性能成本
这篇关于将 bind1st 用于通过引用获取参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 bind1st 用于通过引用获取参数的方法
基础教程推荐
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
