不能将 std::p tr_fun 与引用函数一起使用
Cannot use std::ptr_fun with function that takes reference
不允许使用 C++11 或 Boost。
我正在尝试编译以下代码,但我遇到了问题。 std::ptr_fun
似乎不喜欢作为引用的参数。
#include <algorithm>
#include <functional>
#include <vector>
struct Something
{
};
template<class T>
T Function(const T& x, int s)
{
// blah blah
return x;
}
int main()
{
std::vector<Something> data(20);
std::transform(data.begin(), data.end(), data.begin(), std::bind2nd(std::ptr_fun(Function<Something>), 8));
}
VS2013 错误消息:错误 C2535: 'Something std::binder2nd>::运算符 ()(const Something &) const":已定义或声明的成员函数
但是,如果我将 Function
中的参数更改为T x
它就可以工作了!
有没有办法在不修改Function
的情况下方便地工作?
现场示例:
http://ideone.com/Eno7gF
http://ideone.com/kGmv7r
你不能
这样做。 这是对 std::bind1st 和 std::bind2nd 的基本限制。 问题是它定义了两个()运算符,其中一个已经有const和。 因此,编译器会看到两个相同的函数。 它不会被修复,因为 C++11 已经弃用了这些方法。
另请参阅:
将 bind1st 用于通过引用获取参数的方法
使用 bind2nd() 的奇怪的编译器错误:"成员函数已定义或声明"而不是"引用"
绑定用户定义类的第二个问题
相关文章:
- 区分接受常量参数的函数引用/指针和与函数参数同名的非常量参数
- 类 Referention 中C++回调函数引用非静态函数
- 使用函数引用指向节点的指针删除链表中的节点?
- 解释通过从函数引用返回数组的语法
- "Class1"类"Class2"对象作为私有数据成员。如何通过"Class 2"函数引用"Class1"对象?
- 使用默认构造函数引用成员变量初始化错误
- 无法调用函数引用 c++
- 使用 decltype(this) 获取函数引用
- 我应该如何定义返回指针的函数?(引用指针与指针指针)
- 从内联函数引用文件静态变量
- Boost::将sigaction函数引用绑定到实例
- 一种比函数引用更有效的方法
- 奇怪的未定义函数引用,函数调用C++不存在
- 是否可以检测绑定成员函数引用的对象是否被删除或销毁
- 构造函数引用参数导致seg错误
- 是否可以使函数模板从函数引用中获取“decltype”
- 无法让 Lua 函数引用"self"
- 如何解析变量和函数引用(Linker & Compiler)?
- 从不同模块调用函数 - 引用错误
- 如何在主函数中连接到数据库,然后从其他函数引用它