0

我有以下问题,我找不到解决方案。当然,可能根本不存在解决方案,但我想在放弃之前尝试一下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; };

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

4

2 回答 2

2

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

您可以按照 ForEveR 的解释进行操作,或者您可以使用通用enum_hash模板而不考虑 std 命名空间并将其用于您的数据结构中,因为您不会被迫将其std::hash用作散列函数,例如:

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;
};
于 2015-11-05T14:40:20.923 回答
1

您可以为所有枚举编写散列的专门化,然后一切都可以正常工作。

namespace std {
  template<class E>class hash {
    using sfinae = typename std::enable_if<std::is_enum<E>::value, E>::type;
  public:
    size_t operator()(const E&e) const {
      return std::hash<typename std::underlying_type<E>::type>()(e);
    }
  };
};
于 2015-11-05T14:38:31.393 回答