为什么C++关联式容器默认情况下没有透明谓词?

4
自C++14以来,我们拥有了更透明且在大多数情况下更有用的std::less,因此是否存在理由,例如,默认情况下std::set仍将std::less作为谓词,而不是std::less,除了历史原因外。
有用的情况:std::set::find与std::string_view等。

2
为了不以某种微妙的方式破坏向后兼容性,我做出这个假设。 - HolyBlackCat
1个回答

5

如果这样做会破坏当前的工作代码。想象一下我有

struct my_type
{
    int id;
    int bar;
};

namespace std {
    template<>
    struct less<my_type>
    {
        bool operator()(my_type const& lhs, my_type const& rhs)
        {
            return lhs.id < rhs.id; // bar doesn't need to be compared, only need unique id's in the container.
        }
    };
}

std::set<my_type> foo;

如果将std::set更改为使用std::less<void>,则此代码将不再编译,因为my_type没有operator <

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接