【问题标题】:Clang compiling error on 'std::hash<unsigned long>''std::hash<unsigned long>' 上的 Clang 编译错误
【发布时间】:2013-09-27 02:41:27
【问题描述】:

我正在尝试在我的 mac 上编译一个项目,该项目最初是在 linux 上编写的。它在archlinux上运行顺利,但在mac上有很多错误。特别是,我对这个错误消息感到非常困惑:

In file included from /Users/STW/Documents/neuroblaze/nb/tagged_index/tagged_index.t.hpp:4:
/Users/STW/Documents/neuroblaze/nb/tagged_index/tagged_index.hpp:425:26: error: 
      implicit instantiation of undefined template 'std::hash<unsigned long>'
  ::std::hash<IndexType> hasher;
                         ^

这里是相关代码:(tagged_index.hpp)

namespace std {
/**
 * tagged_index can be hashed. Just forwards the hash to the contained type.
 *
 * @ingroup TaggedIndex
 */
template <typename UniquenessTag, typename IndexType,
          typename ConstructorFunction>
struct hash<tsb::tagged_index<UniquenessTag, IndexType, ConstructorFunction>> {
  using value_type =
      tsb::tagged_index<UniquenessTag, IndexType, ConstructorFunction>;
  ::std::hash<IndexType> hasher;
  size_t operator()(const value_type& l) const { return hasher(l); }
};

/**
 * tagged_offset can be hashed. Just forwards the hash to the contained type.
 *
 * @ingroup TaggedOffset
 */
template <typename UniquenessTag, typename IndexType,
          typename ConstructorFunction>
struct hash<tsb::tagged_offset<UniquenessTag, IndexType, ConstructorFunction>> {
  using value_type =
      tsb::tagged_offset<UniquenessTag, IndexType, ConstructorFunction>;
  ::std::hash<IndexType> hasher;
  size_t operator()(const value_type& l) const { return hasher(l); }
};

} // end namespace std

我在这个 hpp 文件中包含了函数。

【问题讨论】:

  • 你不能向namespace std添加东西。
  • 其实这不是我的代码。它在archlinux上运行得非常好。很奇怪。
  • @n.m.除了模板的特化,例如these
  • 你的 clang 版本是什么?
  • @C.R. clang 3.3 我启用了 c++11 支持

标签: c++ macos c++11 stdhash


【解决方案1】:

你也包括“记忆”吗?我刚刚发现 Clang 的标准库代码中可能存在错误。

哈希的定义如下:

template <class _Tp> struct hash; 

这和__functional_base中的不一样:

template <class _Tp> struct _LIBCPP_TYPE_VIS_ONLY hash;

这可能违反 ODR,具体取决于 _LIBCPP_TYPE_VIS_ONLY 的定义方式。整数类型的 hash 的所有特化都使用该符号,因此重新定义可能会使它们无效。

我发现在记忆后包含功能比在功能后包含记忆效果更好。

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多