本类(class)Age
没有 operator!=
已定义(我无法添加)。
struct Age
{
Age(int i) {integerAge=i; };
int integerAge;
bool operator==(const Age& rhs) const { return this->integerAge==rhs.integerAge; };
//bool operator!=(const Age& rhs) const { return this->integerAge!=rhs.integerAge; }; not availabe
bool operator<(const Age& rhs) const {return this->integerAge<rhs.integerAge;};
};
当我这样做时,c++11 中的代码工作正常:
std::copy_if(v.begin(),v.end(),std::back_inserter(ageof35),boost::bind(&Person::GetAge,_1)==35); //not available in c++03
然而,移植回 c++03 需要表达同样的东西:
std::remove_copy_if(v.begin(), v.end(), std::back_inserter(ageof35),boost::bind(&Person::GetAge,_1)!=35);
现在唯一的区别是 BOOST_BIND_OPERATOR( !=, not_equal )
被要求。
完整示例在这里:http://coliru.stacked-crooked.com/a/f1907a032c397986
在STL中定义operator<
就足够了和 operator==
和 operator!=
只是前两者的组合。但是,boost 绑定(bind)明确需要 operator!=
.
如何强制 boost 绑定(bind)使用已定义 operator==
的否定?
最佳答案
Boost 绑定(bind)不需要任何运算符。它使用您告诉它使用的那些。
因为您有运算符 ==
而不是 !=
,您可以通过组合 !
和 = 来获得您想要的结果=
,即:
!(boost::bind(&Person::GetAge,_1) == 35)
关于c++ - 如何将 BOOST_BIND_OPERATOR( !=, not_equal ) 与未定义 != 运算符的类一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29149922/