簡體   English   中英

std::map 鍵類型的嚴格弱排序概念

[英]Strict weak ordering concept for std::map's key type

我希望這個 class 模板需要一個概念,即 Key 類型支持嚴格的弱排序比較。

template <typename Key>
class Map {
    std::map<Key, int> map;
}

有沒有優雅的解決方案?

這取決於您是想使用<作為比較原語還是需要<=>

如果是前者:

template <typename Key>
    requires std::strict_weak_order<std::less<Key>, Key const&, Key const&>
class Map;

如果是后者:

template <std::three_way_comparable<std::weak_ordering> Key>
class Map;

這兩個都將在編譯時進行語法檢查,但也會對所討論的排序是嚴格的弱順序進行語義檢查(實際上我認為在技術上, weak_ordering措辭中缺少它,但它應該在那里......)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM