不能使用 lambda 作为 unordered_set 对的散列?

Can't use lambda as hash for unordered_set of pairs?

我正在尝试定义一个 unordered_set 超过 unsigned intpair 的类型:

typedef std::pair<unsigned int, unsigned int> Loc;
auto HashLoc = [](const Loc &l) {
  return l.first << 16 ^ l.second;
};
typedef std::unordered_set<Loc, decltype(HashLoc)> LocSet;

这是基于 creating unordered_set with lambda 的公认答案。

当我尝试实际声明该类型的对象时,出现错误 error: call to implicitly-deleted default constructor of 'LocSet'。根据编译器提供的额外细节,看起来它正在尝试为 lambda 调用已删除的默认构造函数。

有趣的是,上面链接的答案表明 VS2013 就是这样做的,这是一个错误,但 gcc 和 clang 应该可以工作。但是,我使用的是 clang(8.0.1 版)。这是预期的行为吗?如果是,那么做我想做的事情的正确方法是什么?

您需要使用 C++20 模式编译,即使用选项 -std=c++2a

在 C++20 之前 lambda 闭包类型没有默认构造函数。

Closure types are not DefaultConstructible. Closure types have no default constructor (until C++20)

If no captures are specified, the closure type has a defaulted default constructor. (since C++20)