簡體   English   中英

內部作用域枚舉,哈希函數和unordered_set數據成員

[英]Inner scoped enumeration, hash function and unordered_set data member

我遇到以下問題,但找不到解決方案。 當然,可能根本就沒有解決方案,但是我想在放棄之前先嘗試一下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 ,或者在這種情況下派生是唯一可行的解​​決方案?

你不能向前聲明嵌套枚舉,看到這個答案。

您可以按照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;
};

您可以為所有枚舉編寫哈希的特殊化,然后一切正常。

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);
    }
  };
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM