此頁面中列出的具名要求,是 C++ 標準的規(guī)范性文本中使用的具名要求,用于定義標準庫的期待。
某些具名要求在 C++20 中正在以概念語言特性進行形式化。在那之前,確保以滿足這些要求的模板實參實例化標準庫模板是程序員的重擔(dān)。若不這么做,則可能導(dǎo)致非常復(fù)雜的編譯器診斷。
全庫范圍的概念
operator== 是一種等價關(guān)系
C++ 具名要求: EqualityComparable
類型必須能使用 == 運算符且結(jié)果應(yīng)當(dāng)具有標準語義。
要求
以下情況下,類型 T
滿足可相等比較 (EqualityComparable) :
給定
-
T
或const T
類型的表達式a
、b
與c
下列表達式必須合法且擁有其指定的效果
表達式 | 返回類型 | 要求 |
---|---|---|
a == b |
可隱式轉(zhuǎn)換為 bool | 建立一種等價關(guān)系,即滿足下列性質(zhì):
|
注解
為滿足此要求,沒有內(nèi)建比較運算符的類型必須提供用戶定義的 operator==。
對于既可相等比較 (EqualityComparable) 又可小于比較 (LessThanComparable) 的類型, C++ 標準庫對相等(即表達式 a == b 的值)和等價(即表達式 !(a < b) && !(b < a) 的值)間做出區(qū)別。
operator< 是一種嚴格弱序關(guān)系
C++ 具名要求: LessThanComparable
類型必須能使用 < 運算符且結(jié)果應(yīng)當(dāng)具有標準語義。
要求
以下情況下,類型 T
滿足LessThanComparable:
給定
-
T
或const T
類型的表達式a
、b
與c
下列表達式必須合法并擁有其指定的效果
表達式 | 返回值 | 要求 |
---|---|---|
a < b | 可隱式轉(zhuǎn)換為 bool | 建立嚴格弱序關(guān)系,即具有下列屬性:
|
注解
為滿足此要求,沒有內(nèi)建比較運算符的類型必須提供用戶定義的 operator<。
對于既可相等比較 (EqualityComparable) 又可小于比較 (LessThanComparable) 的類型,C++ 標準庫在相等(即表達式 a == b 的值)和等價(即表達式 !(a < b) && !(b < a) 的值)間做出區(qū)別。
支持空值的指針式類型
C++ 具名要求: NullablePointer (C++11 起)
指定該類型是能與 std::nullptr_t 對象進行比較的指針式類型。
要求
類型必須滿足所有下列要求:
- 可相等比較 (EqualityComparable)
- 可默認構(gòu)造 (DefaultConstructible)
- 可復(fù)制構(gòu)造 (CopyConstructible)
- 可復(fù)制賦值 (CopyAssignable)
- 可析構(gòu) (Destructible)
此外,此類型的一個值初始化的對象必須產(chǎn)生該類型的空值(null)。空值必須僅與自身等價。該類型的默認初始化可擁有不確定值。
此類型必須可按語境轉(zhuǎn)換成 bool。若其值等價于其空值則此轉(zhuǎn)換的效果為 false,否則為 true。
此類型進行的操作均不可拋異常。
此類型必須滿足下列額外的表達式,給定該類型的兩個值 p
與 q
,以及 np
是 std::nullptr_t 類型的值(可有 const 限定):
表達式 | 效果 |
Type p(np); Type p = np; |
之后 p 等價于 nullptr。 |
Type(np) | 等價于 nullptr 的臨時對象。 |
p = np | 必須返回 Type& ,而且之后 p 等價于 nullptr。 |
p != q | 必須返回能按語境轉(zhuǎn)換成 bool 的值。效果為 !(p == q) 。 |
p == np np == p |
必須返回能按語境轉(zhuǎn)換成 bool 的值。效果為 (p == Type()) 。 |
p != np np != p |
必須返回能按語境轉(zhuǎn)換成 bool 的值。效果為 !(p == np) 。 |
注解
注意,對可空指針 (NullablePointer) 類型不要求解引用(operator*
或 operator->
)。滿足這些要求的最小化類型是文章來源:http://www.zghlxwxcb.cn/news/detail-812670.html
class handle
{
int id;
public:
handle(std::nullptr_t = nullptr) : id(0) { }
explicit operator bool()
{
return id != 0;
}
friend bool operator ==(handle l, handle r)
{
return l.id == r.id;
}
friend bool operator !=(handle l, handle r)
{
return !(l == r);
}
};
標準庫
下列類型必須滿足可空指針 (NullablePointer) :文章來源地址http://www.zghlxwxcb.cn/news/detail-812670.html
- 每個分配器 (Allocator) 類型
X
的成員類型X::pointer
、X::const_pointer
、X::void_pointer
及X::const_void_pointer
- std::unique_ptr 的成員類型
X::pointer
- 類型 std::exception_ptr
調(diào)用示例
#include <iostream>
#include <string>
#include <iomanip>
#include <complex>
#include <tuple>
#include <typeinfo>
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
//類型必須能使用 == 運算符且結(jié)果應(yīng)當(dāng)具有標準語義。
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
bool operator !=(const Cell &cell) const
{
// return x != cell.x && y != cell.y;
return !(*this == cell);
}
//類型必須能使用 < 運算符且結(jié)果應(yīng)當(dāng)具有標準語義。
bool operator <(const Cell &cell) const
{
if (x < cell.x)
{
return true;
}
return y < cell.y;
}
};
class handle
{
int id;
public:
handle(std::nullptr_t = nullptr) : id(0) { }
explicit operator bool()
{
return id != 0;
}
friend bool operator ==(handle l, handle r)
{
return l.id == r.id;
}
friend bool operator !=(handle l, handle r)
{
return !(l == r);
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
int main()
{
std::cout << std::boolalpha;
Cell cell1 = {101, 102};
Cell cell2 = {101, 102};
Cell cell3 = {201, 202};
std::cout << cell1 << " == " << cell2 << " : "
<< (cell1 == cell2) << std::endl;
std::cout << cell1 << " != " << cell2 << " : "
<< (cell1 != cell2) << std::endl;
std::cout << cell1 << " == " << cell3 << " : "
<< (cell1 == cell3) << std::endl;
std::cout << cell1 << " != " << cell3 << " : "
<< (cell1 != cell3) << std::endl;
std::cout << cell1 << " < " << cell2 << " : "
<< (cell1 < cell2) << std::endl;
std::cout << "!(" << cell1 << " < " << cell2 << ") : "
<< !(cell1 < cell2) << std::endl;
std::cout << cell1 << " < " << cell3 << " : "
<< (cell1 < cell3) << std::endl;
std::cout << "!(" << cell1 << " < " << cell3 << ") : "
<< !(cell1 < cell3) << std::endl;
return 0;
}
輸出
{101,102} == {101,102} : true
{101,102} != {101,102} : false
{101,102} == {201,202} : false
{101,102} != {201,202} : true
{101,102} < {101,102} : false
!({101,102} < {101,102}) : true
{101,102} < {201,202} : true
!({101,102} < {201,202}) : false
到了這里,關(guān)于C++ 具名要求-全庫范圍的概念 - 一種等價關(guān)系(operator==)- 是一種嚴格弱序關(guān)系(operator< )的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!