c++ - 内部作用域枚举、散列函数和 unordered_set 数据成员

标签 c++ c++11 unordered-set enum-class stdhash

我有以下问题,但我找不到解决方案。 当然,也有可能根本就没有解决方案,但我还是想尝试一下SO再放弃。

首先,编译没有错误的片段:

#include <unordered_set>
#include <memory>

struct S {
    enum class E: unsigned int { FOO = 0, BAR };
};

namespace std
{
template<>
struct hash<S::E> {
    using argument_type = S::E;
    using underlying_type = std::underlying_type<argument_type>::type;
    using result_type = std::size_t;

    result_type operator()(argument_type const &s) const noexcept {
        const underlying_type us = static_cast<underlying_type>(s);
        hash<underlying_type> hfn;
        return hfn(us);
    }
};
}

int main() {
    std::unordered_set<S::E> set;
}

考虑到这段代码,我发现自己需要将 unordered_set 作为 S 的数据成员,或者至少是派生类。一个可行的解决方案是在 std 命名空间关闭后添加以下行:

struct D: public S {
    std::unordered_set<S::E> set;
};

另一种可能的解决方案是(我还没有尝试过)使用无范围枚举。不管怎样,我做的第一次尝试是修改 struct S 的定义,如下所示:

struct S {
    enum class E: unsigned int { FOO = 0, BAR };
    std::unordered_set<E> set;
};

这以错误结束,因为(如果我正确理解了问题的话)unordered_set 需要专门的 hash 函数。无论如何,后者至少需要声明S::E,因此交换两段代码是不够的。

这是错误日志的第一部分(因为它很长):

In file included from /usr/include/c++/5/bits/hashtable.h:35:0,
                 from /usr/include/c++/5/unordered_set:47,
                 from main.cpp:1:
/usr/include/c++/5/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash<S::E, std::hash<S::E> >’:
/usr/include/c++/5/type_traits:137:12:   required from ‘struct std::__and_<std::__is_fast_hash<std::hash<S::E> >, std::__detail::__is_noexcept_hash<S::E, std::hash<S::E> > >’
/usr/include/c++/5/type_traits:148:38:   required from ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<S::E> >, std::__detail::__is_noexcept_hash<S::E, std::hash<S::E> > > >’
/usr/include/c++/5/bits/unordered_set.h:95:63:   required from ‘class std::unordered_set<S::E>’
main.cpp:6:27:   required from here
/usr/include/c++/5/bits/hashtable_policy.h:85:34: error: no match for call to ‘(const std::hash<S::E>) (const S::E&)’
  noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
                                  ^
In file included from /usr/include/c++/5/bits/move.h:57:0,
                 from /usr/include/c++/5/bits/stl_pair.h:59,
                 from /usr/include/c++/5/utility:70,
                 from /usr/include/c++/5/unordered_set:38,
                 from main.cpp:1:
/usr/include/c++/5/type_traits: In instantiation of ‘struct std::__not_<std::__and_<std::__is_fast_hash<std::hash<S::E> >, std::__detail::__is_noexcept_hash<S::E, std::hash<S::E> > > >’:
/usr/include/c++/5/bits/unordered_set.h:95:63:   required from ‘class std::unordered_set<S::E>’
main.cpp:6:27:   required from here
/usr/include/c++/5/type_traits:148:38: error: ‘value’ is not a member of ‘std::__and_<std::__is_fast_hash<std::hash<S::E> >, std::__detail::__is_noexcept_hash<S::E, std::hash<S::E> > >’
     : public integral_constant<bool, !_Pp::value>
                                      ^
In file included from /usr/include/c++/5/unordered_set:48:0,
                 from main.cpp:1:
/usr/include/c++/5/bits/unordered_set.h: In instantiation of ‘class std::unordered_set<S::E>’:
main.cpp:6:27:   required from here
/usr/include/c++/5/bits/unordered_set.h:95:63: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<S::E> >, std::__detail::__is_noexcept_hash<S::E, std::hash<S::E> > > >’
       typedef __uset_hashtable<_Value, _Hash, _Pred, _Alloc>  _Hashtable;
                                                               ^
/usr/include/c++/5/bits/unordered_set.h:102:45: error: ‘value’ is not a member of ‘std::__not_<std::__and_<std::__is_fast_hash<std::hash<S::E> >, std::__detail::__is_noexcept_hash<S::E, std::hash<S::E> > > >’
       typedef typename _Hashtable::key_type key_type;

通常,在这种情况下,我可以使用前向声明之类的东西来解决,如下例所示:

struct B;
struct A { B *link; };
struct B { A *link; };

不幸的是,我无法对嵌入到 struct 中的 enum 做类似的事情,这就是我提出这个问题的原因。是否有可能解决它,从而避免定义派生类 D,或者在这种情况下派生是唯一可行的解​​决方案?

最佳答案

您不能转发声明嵌套枚举,请参阅 this回答。

您可以按照 ForEveR 的说明进行操作,或者您可以拥有通用的 enum_hash 模板,而不考虑 std 命名空间,并在您的数据结构中使用它,因为您不会被迫使用 std::hash 作为哈希函数,eg:

template<typename T>
struct enum_hash {
  using argument_type = T;
  using underlying_type = typename std::underlying_type<argument_type>::type;
  using result_type = std::size_t;

  result_type operator()(argument_type const &s) const noexcept {
    const underlying_type us = static_cast<underlying_type>(s);
    std::hash<underlying_type> hfn;
    return hfn(us);
  }

  static_assert(std::is_enum<T>::value, "T must be an enum!");
};

struct S {
  enum class E: unsigned int { FOO = 0, BAR };
  std::unordered_set<S::E, enum_hash<S::E>> set;
};

关于c++ - 内部作用域枚举、散列函数和 unordered_set 数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33547041/

相关文章:

在 unordered_sets 上排序

c++ - Visual Studio Express 2012 中未显示 TODO 项目

c++ - 即使在设计器中更改高度后,组合框下拉菜单也不起作用

c++ - constexpr 函数不需要返回常量表达式吗?

c++ - map 与 map 中可接受的键类型无序映射

c++ - 使用 `std::min` 作为算法参数

C++阅读播放列表没有专辑的特定分隔符

performance - 无序集(const char)比无序集(字符串)慢得多

c++ - std::unordered_multiset::find 函数是否返回具有相同散列值的两个值之间的第一个插入元素

c++ - 当我尝试从vbo中获取颜色时,glDrawElements没有输出