2013-07-23 77 views
6

如果我已经运营商>操作<定义(和==操作符),做我需要定义运营商> =操作< =,或将编译器宣布他们为我如果我故意不宣布他们?我是否需要手动声明> =和<=运算符?

另外,如果我有定义==操作符,将编译器宣布操作!=我

+2

如果您定义了'operator <'和'operator ==',你可以拖拽['std :: rel_ops'](http://en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp)命名空间来提供其余的运营商,但这不是一个[很好的解决方案](http://stackoverflow.com/questions/62 25375 /惯用 - 使用 - 的-stdrel-OPS)。 [Boost.Operators](http://stackoverflow.com/a/14756785/241631)是正确的方法。 – Praetorian

+0

某些东西不能与我一起使用继承来保存输入一些操作符。宁愿手动添加它们。 =) –

+0

派生自提供该功能的类也具有很好的文档价值。它是概念的一种原始形式。 – TemplateRex

回答

6

不,编译器不会声明/定义您没有手动定义的任何运算符。然而,Boost.Operators可能会满足您的需求 - 它完全符合您希望编译器执行的操作。

+0

当。他们生成体面的默认复制构造函数等。希望有一个简单的自动生成*运算符!=(other){return!(* ​​this == other); } *。哦,谢谢你的回答!在接受之前会稍等一会。 –

+1

+1 Boost.Operators(Daniel Frey)的维护者拥有一个名为[df.operators](https://github.com/d-frey/operators)的非官方端口到C++ 11(右值重载和noexcept) – TemplateRex

5

编译器不会为你做任何事情本身在这里,但它是 相对简单的由 继承适当的类自动生成,这样的:

template< typename DerivedType > 
class ComparisonOperators 
{ 
public: 

    friend bool   operator!=( 
          DerivedType const& lhs, 
          DerivedType const& rhs) 
    { 
     return !(lhs == rhs); 
    } 

    friend bool   operator<=( 
          DerivedType const& lhs, 
          DerivedType const& rhs) 
    { 
     return !(rhs < lhs); 
    } 

    friend bool   operator>( 
          DerivedType const& lhs, 
          DerivedType const& rhs) 
    { 
     return rhs < lhs; 
    } 

    friend bool   operator>=( 
          DerivedType const& lhs, 
          DerivedType const& rhs) 
    { 
     return !(lhs < rhs); 
    } 

protected: 
         ~ComparisonOperators() {} 
} ; 

定义在类<==,而源于此,而 你会得到所有的运营商:

class MyClass : public ComparisonOperators<MyClass> 
{ 
    // ... 
public: 
    bool operator==(MyClass const& other) const; 
    bool operator<(MyClass const& other) const; 
    // ... 
}; 

只是注意:我手动简化我ACTU版本人使用, 限定==<以及,将查找构件 功能compareisEqual和,并使用compare==!=当没有isEqual。我不认为我有 引入了任何错误,但你永远不知道。

+0

我倾向于使用返回负值,正值或0值(传统C方法)的'int compare(DerivedType const&o)'。通常'=='和'<'是非常相似的代码,所以合并它们很不错。 –

+3

这不正是Boost.Operators已经为你做了什么吗? ;-) –

+0

@ edA-qamort-ora-y是的。这是上面“比较”的签名。规则是:只对比较等式:'isEqual'(如果使用它们,关系运算符将无法编译);平等和关系,使用'比较'; _if_平等可以做得更快,那么你可以提供两者。在'ComparisonOperators'类中有一些元编程可以调用正确的编程。 –

0

这里已经有一些很好的答案,使用boost和继承。但正如有人指出的那样 - 使用继承来创建操作符似乎是错误的。

我知道#define s在C++中是“禁忌”,但这仍然是我在这里使用的。

我在一般用途有#define包括是这样的:

#define CREATE_COMPARITORS(name)        \ 
inline bool operator>(const name &o){return o<*this;}   \ 
inline bool operator<=(const name &o){return not (o<*this);} \ 
inline bool operator>=(const name &o){return not (*this<o);} \ 
inline bool operator!=(const name &o){return not (*this==o);} 

那么,如果我有一个类,所有我需要声明是operator<operator==

class ttt{ 
    //... 
    bool operator<(const ttt &o); 
    bool operator==(const ttt &o); 
    CREATE_COMPARITORS(ttt); 
    //... 
}; 
+0

你可能需要证明这一点“似乎......错了。“到目前为止,我相信提升图书馆设计师不仅仅是你的不合理的观点。 – djechlin

+0

引用了这个帖子中的其他人,他说:“有些东西不能与我一起使用继承来节省输入一些操作符。”现在,如果你想让我失望,因为我的回答没有帮助,或者没有添加任何东西,那么很好。但是如果你因为我是一大群不喜欢'Boost'并且它是如何做事的人中的一员而使我失望的,而你碰巧是另一个喜欢'Boost'的人的一部分,那么你就是滥用你的权力。 – rabensky

+0

-1用于推荐使用#define over继承,没有任何理由认为继承存在问题,或者为什么此代码免于使用宏的常见问题。 – djechlin

相关问题