## Operators
### 頭文件: `"boost/operators.hpp"`
Operators庫由多個基類組成。每一個類生成與其名字概念相關的操作符。你可以用繼承的方式來 使用它們,如果你需要一個以上的功能,則需要使用多重繼承。幸運的是,Operators中定義了一些復合的概念,在大多數情況下可以無須使用多重繼承。 下面將介紹最常用的一些Operator類,包括它們所表示的概念,以及它們對派生類的要求。某些情況下,使用Operators時,對真實概念的要求會 不同于對該概念基類的要求。例如,概念 addable 要求有一個操作符 `T operator+(const T& lhs,const T& rhs)` 的定義,而Operators的基類 `addable` 卻要求有一個成員函數,`T operator+=(const T& other)`. 使用這個成員函數,基類 `addable` 為派生類自動增加了 `operator+`. 在以下章節中,都是首先給出概念,然后再給出對派生自該概念的類的要求。我沒有重復本庫中的所有概念,僅是挑選了最重要的一些;你可以在 [www.boost.org](http://www.boost.org) 上找到完整的參考文檔。
#### less_than_comparable
`less_than_comparable` 要求類型`T`具有以下語義。
```
bool operator<(const T&,const T&);
bool operator>(const T&,const T&);
bool operator<=(const T&,const T&);
bool operator>=(const T&,const T&);
```
要派生自 `boost::less_than_comparable`, 派生類(`T`)必須提供:
```
bool operator<(const T&, const T&);
```
注意,返回值的類型不必是真正的 `bool`, 但必須可以隱式轉換為 `bool`.?C++標準中的概念 LessThanComparable 要求提供`operator<` ,所以從 `less_than_comparable` 派生的類必須符合該要求。作為回報,`less_than_comparable` 將依照 `operator<` 實現其余的三個操作符。
#### equality_comparable
`equality_comparable`?要求類型`T`具有以下語義。
```
bool operator==(const T&,const T&);
bool operator!=(const T&,const T&);
```
要派生自?`boost::equality_comparable`,?派生類(`T`)必須提供:
```
bool operator==(const T&,const T&);
```
同樣,返回值的類型不必是 `bool`, 但必須可以隱式轉換為 `bool`. C++標準中的概念 EqualityComparable 要求必須提供 `operator==` ,因此從 `equality_comparable` 派生的類必須符合該要求。`equality_comparable` 類為 `T` 提供 `bool operator!=(const T&,const T&)`.
#### addable
`addable` 概念要求類型`T`具有以下語義。
```
T operator+(const T&,const T&);
T operator+=(const T&);
```
要派生自 `boost::addable`,?派生類(`T`)必須提供:
```
T operator+=(const T&);
```
返回值的類型必須可以隱式轉換為 `T`. 類 `addable` 為 `T` 實現 `T operator+(const T&,const T&)`.
#### subtractable
`subtractable`?概念要求類型`T`具有以下語義。
```
T operator-(const T&,const T&);
T operator-=(const T&); // 譯注:原文為 T operator+=(const T&); 有誤
```
要派生自 `boost::subtractable`,?派生類(`T`)必須提供:
```
T operator-=(const T&,const T&);
```
返回值的類型必須可以隱式轉換為 `T`. 類 `addable` 為 `T` 實現 `T operator-(const T&,const T&)`.
#### orable
`orable`?概念要求類型`T`具有以下語義。
```
T operator|(const T&,const T&);
T operator|=(const T&,const T&);
```
要派生自?`boost::orable`,?派生類(`T`)必須提供:
```
T operator|=(const T&,const T&);
```
返回值的類型必須可以隱式轉換為 `T`. 類 `addable` 為 `T` 實現?`T operator|(const T&,const T&)`.
#### andable
`andable`?概念要求類型`T`具有以下語義。
```
T operator&(const T&,const T&);
T operator&=(const T&,const T&);
```
要派生自?`boost::andable`,?派生類(`T`)必須提供:
```
T operator&=(const T&,const T&);
```
返回值的類型必須可以隱式轉換為 `T`. 類 `addable` 為 `T` 實現?`T operator&(const T&,const T&)`.
#### incrementable
`incrementable`?概念要求類型`T`具有以下語義。
```
T& operator++(T&);
T operator++(T&,int);
```
要派生自?`boost::incrementable`,?派生類(`T`)必須提供:
```
T& operator++(T&);
```
返回值的類型必須可以隱式轉換為 `T`. 類 `addable` 為 `T` 實現?`T operator++(T&,int)`.
#### decrementable
`decrementable`?概念要求類型`T`具有以下語義。
```
T& operator--(T&);
T operator--(T&,int);
```
要派生自?`boost::decrementable`,?派生類(`T`)必須提供:
```
T& operator--(T&);
```
返回值的類型必須可以隱式轉換為 `T`. 類 `addable` 為 `T` 實現?`T operator--(T&,int)`.
#### equivalent
`equivalent`?概念要求類型`T`具有以下語義。
```
bool operator<(const T&,const T&);
bool operator==(const T&,const T&);
```
要派生自?`boost::equivalent`,?派生類(`T`)必須提供:
```
bool operator<(const T&,const T&);
```
返回值的類型必須可以隱式轉換為?`bool`. 類 `equivalent` 為 `T` 實現 `T operator==(const T&,const T&)`. 注意,等價(equivalence)和相等(equality)準確的說是不一樣的;兩個等價(equivalent)的對象并不一定是相等的(equal)。但對于這里的 `equivalent` 概念而言,它們是一樣的。
### 解引用操作符
對于迭代器,有兩個概念特別有用,`dereferenceable` 和 `indexable`, 分別表示了解引用的兩種情況:一個是`*t`,?`t`是一個支持解引用的迭代器(顯然所有迭代器都支持),另一個是indexing, `t[x]`,?`t`是一個支持下標操作符尋址的類型,而 `x` ?通常是一個整數類型。在更高的抽象級別,它們兩個通常一起使用,合稱迭代器操作符,包括這兩個解引用操作符和一些簡單的算術操作符。
#### dereferenceable
`dereferenceable`?概念要求類型`T`具有以下語義,假設 `T` 是操作數,`R` 是引用類型,而 `P` 是指針類型(例如,`T` 是一個迭代器類型,`R` 是該迭代器的`value_type`的引用,而 `P` 則是該迭代器的`value_type`的指針)。
```
P operator->() const;
R operator*() const;
```
要派生自 `boost::dereferenceable`,?派生類(`T`)必須提供:
```
R operator*() const;
```
另外,`R`的一元 `operator&` 必須可以被隱式轉換為 `P`. 這意味著 `R` 不必一定要是引用類型,它可以是一個代理類(proxy class)。類`dereferenceable`?為 `T` 實現 `P operator->() const`.
#### indexable
`indexable` 概念要求類型`T`具有以下語義,假設 `T` 是操作數,`R` 是引用類型,`P` 是指針類型,而 `D` 是 `difference_type` (例如,`T` 是一個迭代器類型,`R` 是該迭代器的`value_type`的引用,`P`?是該迭代器的`value_type`的指針,而 `D` 則是 `difference_type`)。
```
R operator[](D) const;
R operator+(const T&,D);
```
要派生自?`boost::indexable`,?派生類(`T`)必須提供:
```
R operator+(const T&,D);
```
類 `indexable` 為 `T` 實現 `R operator[](D) const`.
### 復合算術操作符
到目前為止我們看到的概念都只代表了最簡單的功能。但是,還有一些高級的,或是復合的概念,它們由幾個簡單概念組合而成,或是在復合概念之上再增加簡單的概念而成。例如,一個類是 `totally_ordered` 的,如果它同時是 `less_than_comparable` 的和 `equality_comparable`的。這些組合很有用,因為它們減少了代碼的數量,同時還表明了重要且常用的概念。由于它們只是表示了已有概念的組合,所以這些概念很容易用一個表格來表示它們所包含的簡單概念。例如,如果一個類派生自 `totally_ordered`, 它必須實現 `less_than_comparable` 所要求的操作符(`bool operator<(const T&,const T&)`) 和 `equality_comparable` 所要求的操作符(`bool operator==(const T&,const T&)`)。
| 組合概念 | 由以下概念組成 |
| --- | --- |
| totally_ordered | less_than_comparableequality_comparable |
| additive | addablesubtractable |
| multiplicative | multipliabledividable |
| integer_multiplicative | multiplicativemodable |
| arithmetic | additivemultiplicative |
| integer_arithmetic | additiveinteger_multiplicative |
| bitwise | andableorablexorable |
| unit_steppable | incrementabledecrementable |
| shiftable | left_shiftableright_shiftable |
| ring_operators | additivemultipliable |
| ordered_ring_operators | ring_operatorstotally_ordered |
| field_operators | ring_operatorsdividable |
| ordered_field_operators | field_operatorstotally_ordered |
| euclidian_ring_operators | ring_operatorsdividablemodable |
| ordered_ euclidian_ring_operators | euclidean_ring_operatorstotally_ordered |
- 序
- 前言
- Acknowledgments
- 關于作者
- 本書的組織結構
- Boost的介紹
- 字符串及文本處理
- 數 據結構, 容器, 迭代器, 和算法
- 函數對象及高級編程
- 泛 型編程與模板元編程
- 數學及數字處理
- 輸入/輸出
- 雜項
- Part I: 通用庫
- Library 1. Smart_ptr
- Smart_ptr庫如何改進你的程序?
- 何時我們需要智能指針?
- Smart_ptr如何適應標準庫?
- scoped_ptr
- scoped_array
- shared_ptr
- shared_array
- intrusive_ptr
- weak_ptr
- Smart_ptr總結
- Library 2. Conversion
- Conversion 庫如何改進你的程序?
- polymorphic_cast
- polymorphic_downcast
- numeric_cast
- lexical_cast
- Conversion 總結
- Library 3. Utility
- Utility 庫如何改進你的程序?
- BOOST_STATIC_ASSERT
- checked_delete
- noncopyable
- addressof
- enable_if
- Utility 總結
- Library 4. Operators
- Operators庫如何改進你的程序?
- Operators
- 用法
- Operators 總結
- Library 5. Regex
- Regex庫如何改進你的程序?
- Regex 如何適用于標準庫?
- Regex
- 用法
- Regex 總結
- Part II: 容器及數據結構
- Library 6. Any
- Any 庫如何改進你的程序?
- Any 如何適用于標準庫?
- Any
- 用法
- Any 總結
- Library 7. Variant
- Variant 庫如何改進你的程序?
- Variant 如何適用于標準庫?
- Variant
- 用法
- Variant 總結
- Library 8. Tuple
- Tuple 庫如何改進你的程序?
- Tuple 庫如何適用于標準庫?
- Tuple
- 用法
- Tuple 總結
- Part III: 函數對象與高級編程
- Library 9. Bind
- Bind 庫如何改進你的程序?
- Bind 如何適用于標準庫?
- Bind
- 用法
- Bind 總結
- Library 10. Lambda
- Lambda 庫如何改進你的程序?
- Lambda 如何適用于標準庫?
- Lambda
- 用法
- Lambda 總結
- Library 11. Function
- Function 庫如何改進你的程序?
- Function 如何適用于標準庫?
- Function
- 用 法
- Function 總結
- Library 12. Signals
- Signals 庫如何改進你的程序?
- Signals 如何適用于標準庫?
- Signals
- 用法
- Signals 總結