【问题标题】:Strict weak ordering concept for std::map's key typestd::map 键类型的严格弱排序概念
【发布时间】:2020-05-26 11:19:21
【问题描述】:

我希望这个类模板需要一个概念,以便 Key 类型支持严格的弱排序比较。

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

有没有优雅的解决方案?

【问题讨论】:

  • std::strict_weak_order?虽然不知道具体怎么用。
  • 我试过使用它,但不能让它工作。
  • 你用的是哪个编译器?哪个版本?
  • @Damien gcc 10.1.0
  • 您能做的最好的事情就是记录下来。无法以编程方式检查。您必须检查每对可能的输入。

标签: c++ templates c++20 c++-concepts


【解决方案1】:

这取决于您是想使用&lt; 作为比较原语还是需要&lt;=&gt;

如果是前者:

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 措辞在技术上缺少这一点,但它应该在那里.. .)

【讨论】:

    猜你喜欢
    • 2013-02-14
    • 2015-09-20
    • 2018-08-04
    • 1970-01-01
    • 2010-11-20
    • 2016-02-04
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    相关资源
    最近更新 更多