国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C++ 具名要求-全庫范圍的概念 - 一種等價關(guān)系(operator==)- 是一種嚴格弱序關(guān)系(operator< )

這篇具有很好參考價值的文章主要介紹了C++ 具名要求-全庫范圍的概念 - 一種等價關(guān)系(operator==)- 是一種嚴格弱序關(guān)系(operator< )。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

此頁面中列出的具名要求,是 C++ 標準的規(guī)范性文本中使用的具名要求,用于定義標準庫的期待。

某些具名要求在 C++20 中正在以概念語言特性進行形式化。在那之前,確保以滿足這些要求的模板實參實例化標準庫模板是程序員的重擔(dān)。若不這么做,則可能導(dǎo)致非常復(fù)雜的編譯器診斷。

全庫范圍的概念

operator== 是一種等價關(guān)系

C++ 具名要求: EqualityComparable

類型必須能使用 == 運算符且結(jié)果應(yīng)當(dāng)具有標準語義。

要求

以下情況下,類型 T 滿足可相等比較 (EqualityComparable)

給定

  • Tconst T 類型的表達式 a、bc

下列表達式必須合法且擁有其指定的效果

表達式 返回類型 要求
a == b 可隱式轉(zhuǎn)換為 bool 建立一種等價關(guān)系,即滿足下列性質(zhì):
  • 對于 a 的所有值,a == a 產(chǎn)生 true;
  • 若 a == b,則 b == a;
  • 若 a == b 且 b == c,則 a == c。

注解

為滿足此要求,沒有內(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:

給定

  • Tconst T 類型的表達式 a、bc

下列表達式必須合法并擁有其指定的效果

表達式 返回值 要求
a < b 可隱式轉(zhuǎn)換為 bool 建立嚴格弱序關(guān)系,即具有下列屬性:
  • 對于所有 a,!(a < a)
  • 若 a < b 則 !(b < a)
  • 若 a < b 且 b < c 則 a < c
  • 定義 equiv(a, b) 為 !(a < b) && !(b < a),若 equiv(a, b) 且 equiv(b, c),則 equiv(a, c)

注解

為滿足此要求,沒有內(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。

此類型進行的操作均不可拋異常。

此類型必須滿足下列額外的表達式,給定該類型的兩個值 pq,以及 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->)。滿足這些要求的最小化類型是

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::pointerX::const_pointer、X::void_pointerX::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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包