我的C++bind2nd

原版C++bind2nd

我的C++bind2nd

主要是为了理解C++的仿函数:

template<typename Operation>
class mybinder2nd : public unary_function<typename Operation::first_argument_type,typename Operation::result_type> {
protected:
    Operation op;
    typename Operation::second_argument_type value;
public:
    mybinder2nd(const Operation & x,const typename Operation::second_argument_type& y) :op(x),value(y){}
    typename Operation::result_type operator()(typename Operation::first_argument_type & x) const {
        return op(x,value);
    }
    typename Operation::result_type operator()(const typename Operation::first_argument_type & x) const {
        return op(x,value);
    }
};

template<typename Operation,typename T>
inline mybinder2nd<Operation> mybind2nd(const Operation & op,const T & t) {
    typedef typename Operation::second_argument_type arg2_type;
    return mybinder2nd<Operation>(op,arg2_type(t));
}


用起来:

int main()
{
    vector<int>vt = {1,2,5,6,710,12,15};
    sort(vt.begin(),vt.end(),myless<int>());
    for(auto i : vt) cout<<i<<' ';
	cout<< count_if(vt.begin(),vt.end(),not1(mybind2nd(less<int>(),40)));
    return 0;
}

结果跟原版的一样。
讲讲调用过程:
count_if函数传进参数是两个迭代器和一个仿函数,意思是从头到尾遍历元素,返回满足仿函数条件的值的个数,关于迭代器的就不讲了,讲讲如何调用mybind2nd:传进(less<int>(),40),就会调用:mybinder2nd<Operation> mybind2nd(const Operation & op,const T & t),此函数是返回mybinder2nd<Operation>(op,t);,此时进入到mybinder2nd类mybinder2nd中有两个数据op和value,先初始化对象,然后则是调用相应的操作符重载函数返回结果,这里()操作符重载返回的结果就是less<int>(x,40);,需要注意的是less<int>里面的first_argument_type,second_argument_type,result_type等变量(继承于binary_function),如果错了,则无法编译成功。

在 Qt Mac 环境中使用 Boost 库时出现 `no member named bind2nd` 的编译错误,通常是由于项目中启用了 C++17 或更高版本的标准,并且 Boost 版本较低,未对弃用的 `bind2nd` 接口进行兼容处理。 `bind2nd` 是 C++98/03 标准库中的一个函数模板,用于绑定二元函数对象的第二个参数。然而,在 C++17 中,`bind2nd` 被正式移除,取而代之的是使用 `std::bind` 或 Lambda 表达式[^1]。如果 Boost 库的代码中仍然使用了 `bind2nd`,并且项目启用了 C++17 标准,则会出现该错误。 解决此问题的方法包括: ### 1. 降级 C++ 标准版本 在 `.pro` 文件中将 C++ 标准指定为 C++14,以避免使用 C++17 中被移除的接口: ```qmake QMAKE_CXXFLAGS += -std=c++14 ``` 该方式可以确保 Boost 代码中使用 `bind2nd` 的部分仍然能够正常编译通过。 ### 2. 升级 Boost 库版本 确保使用的 Boost 版本支持 C++17 标准并已修复对 `bind2nd` 的兼容性问题。Boost 1.66 及以上版本对 C++17 有更好的支持,部分模块已移除对 `bind2nd` 的依赖或进行了适配。 ### 3. 替换代码中的 `bind2nd` 如果 Boost 库或项目代码中仍然使用 `bind2nd`,建议将其替换为 `std::bind` 或 Lambda 表达式。例如: ```cpp // 原始使用 bind2nd 的方式 std::transform(vec.begin(), vec.end(), std::back_inserter(result), std::bind2nd(std::multiplies<int>(), 2)); // 使用 std::bind 替代 std::transform(vec.begin(), vec.end(), std::back_inserter(result), std::bind(std::multiplies<int>(), std::placeholders::_1, 2)); ``` 或使用 Lambda 表达式: ```cpp std::transform(vec.begin(), vec.end(), std::back_inserter(result), [](int x) { return x * 2; }); ``` ### 4. 设置编译器兼容性标志 如果 Boost 库版本较低但需要在 C++17 下编译,可尝试添加 `_GLIBCXX_USE_CXX11_ABI=0` 标志以启用兼容性模式(适用于 GCC)或 `-D_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS`(适用于 Clang): ```qmake QMAKE_CXXFLAGS += -std=c++17 -D_LIBCPP_ENABLE_CXX17_REMOVED_BINDERS ``` 该方法允许在 C++17 模式下使用 `bind2nd` 接口,但不推荐长期使用。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值