【问题标题】:Does the c++ specify a hash function for references?c++ 是否为引用指定哈希函数?
【发布时间】:2015-11-29 13:13:41
【问题描述】:

我的问题很快。

C++ 标准库是否为引用实现哈希函数,如std::hash<T&>

【问题讨论】:

  • 巴里是对的;这提出的问题比它提出的要多:引用的散列应该做什么?它应该传播引用值的散列吗?还是应该散列引用“本身”,即对象所在的地址?在不同的情况下,两者都可能是可取的,例如按地址散列可以避免使用指针,其中键是特定实例,而不仅仅是它的表观值(如果它甚至 具有 任何值语义)。也许委员会认为用户只需几行代码就可以轻松编写他们想要的任何语义,而不是标准化两个变体

标签: c++ c++11 std standard-library


【解决方案1】:

不,它没有。 [function.objects] 中列举的特化有:

// Hash function specializations
template <> struct hash<bool>;
template <> struct hash<char>;
template <> struct hash<signed char>;
template <> struct hash<unsigned char>;
template <> struct hash<char16_t>;
template <> struct hash<char32_t>;
template <> struct hash<wchar_t>;
template <> struct hash<short>;
template <> struct hash<unsigned short>;
template <> struct hash<int>;
template <> struct hash<unsigned int>;
template <> struct hash<long>;
template <> struct hash<long long>;
template <> struct hash<unsigned long>;
template <> struct hash<unsigned long long>;

template <> struct hash<float>;
template <> struct hash<double>;
template <> struct hash<long double>;

template<class T> struct hash<T*>;

此外,这样的事情甚至意味着什么?

【讨论】:

  • 顺便说一句,UB 将它(作为用户)专门用于T&amp;
猜你喜欢
  • 2016-09-22
  • 1970-01-01
  • 2011-02-04
  • 2012-09-19
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 2015-07-04
  • 2019-03-19
相关资源
最近更新 更多