【问题标题】:Using C++11 unordered_set in Visual C++ and clang在 Visual C++ 和 clang 中使用 C++11 unordered_set
【发布时间】:2013-07-26 23:19:39
【问题描述】:

我正在尝试在跨平台 C++ 应用程序中使用 std::unordered_set。它在 Windows 下的 Visual C++ 中编译和工作就像一个魅力,但在 Mac OS X 下的 clang 上会产生一个致命的编译错误。

我想知道为什么会发生这种情况,以及什么是让它发挥作用的正确方法。

示例代码:

// 
// Clang build cmdline:
// $ clang++ ./set.cpp -Wall -Werror -Wfatal-errors -std=c++11 -stdlib=libc++ -o set.out
// 

#include <iostream>
#include <unordered_set>

struct Point {
    int x, y;
    Point(int x = 0, int y = 0) {
        this->x = x;
        this->y = y;
    }
    bool operator==(Point const& p) const {
        return this->x == p.x && this->y == p.y;
    }
    operator std::size_t () const {
        return std::hash<int>()(x) ^ std::hash<int>()(y);
    }
};

typedef std::unordered_set<Point> points_set_t;

int main() {
    Point point1(1, 5);
    Point point2(1, 1);
    Point point3(1, 5);
    points_set_t points;
    points.insert(point1);
    points.insert(point2);
    points.insert(point3);
    for (points_set_t::const_iterator it = points.begin(); it != points.end(); it++) {
        std::cout << it->x << ":" << it->y << std::endl;
    }
}

Clang 输出:

In file included from ./set.cpp:6:
In file included from /usr/bin/../lib/c++/v1/iostream:38:
In file included from /usr/bin/../lib/c++/v1/ios:216:
In file included from /usr/bin/../lib/c++/v1/__locale:15:
In file included from /usr/bin/../lib/c++/v1/string:434:
In file included from /usr/bin/../lib/c++/v1/algorithm:591:
/usr/bin/../lib/c++/v1/type_traits:748:38: fatal error: implicit instantiation of undefined template 'std::__1::hash<Point>'
    : public integral_constant<bool, __is_empty(_Tp)> {};
                                     ^
/usr/bin/../lib/c++/v1/memory:1948:40: note: in instantiation of template class 'std::__1::is_empty<std::__1::hash<Point> >'
      requested here
                                bool = is_empty<_T2>::value
                                       ^
/usr/bin/../lib/c++/v1/memory:1970:44: note: in instantiation of default argument for '__libcpp_compressed_pair_switch<unsigned
      long, std::__1::hash<Point>, false, false>' required here
template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
                                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/memory:2354:15: note: in instantiation of default argument for '__libcpp_compressed_pair_imp<unsigned long,

      std::__1::hash<Point> >' required here
    : private __libcpp_compressed_pair_imp<_T1, _T2>
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/__hash_table:527:55: note: in instantiation of template class 'std::__1::__compressed_pair<unsigned long,
      std::__1::hash<Point> >' requested here
    __compressed_pair<size_type, hasher>              __p2_;
                                                      ^
/usr/bin/../lib/c++/v1/unordered_set:330:13: note: in instantiation of template class 'std::__1::__hash_table<Point,
      std::__1::hash<Point>, std::__1::equal_to<Point>, std::__1::allocator<Point> >' requested here
    __table __table_;
            ^
./set.cpp:28:18: note: in instantiation of template class 'std::__1::unordered_set<Point, std::__1::hash<Point>,
      std::__1::equal_to<Point>, std::__1::allocator<Point> >' requested here
    points_set_t points;
                 ^
/usr/bin/../lib/c++/v1/memory:3076:29: note: template is declared here
template <class _Tp> struct hash;
                            ^
1 error generated.      

UPD 使用@mfontanini 的建议的工作实现:https://gist.github.com/vbo/6090142。

【问题讨论】:

  • 你需要提供一个哈希函数/函子。

标签: c++ visual-c++ c++11 clang unordered-set


【解决方案1】:

为了使 std::unordered_set 与您的 Point 类一起工作,您可以为其提供 std::hash 专业化:

namespace std
{
template<>
struct hash<Point> {
    size_t operator()(const Point &pt) const {
        return std::hash<int>()(pt.x) ^ std::hash<int>()(pt.y);
    }
};
}

您还可以更改std::unordered_set 的第二个模板参数(默认为std::hash&lt;Point&gt;),它表示返回所需哈希的函子类型。

您似乎尝试通过用户定义的转换为size_t 来提供此哈希实现,但这不起作用。它在 VC 中工作的事实是由它们的实现中的一些错误引起的。

【讨论】:

  • @vbo 显然,VC 的实现有问题,使您的用户定义的转换工作。
  • @vbo:因为 Visual Studio 显然在做不应该做的事情。 (很可能,定义一个无用的主要 hash 模板实现,或者可能使用您的转换运算符,但这只是一个猜测)。
  • @mfontanini 请在答案本身中添加一个关于奇怪的 Visual C++ 行为的句子。不是每个人都会读 cmets。
  • 我认为专攻std::hash是多余的。您可以(甚至必须)为哈希编写自己的用户类。
猜你喜欢
  • 2017-03-20
  • 2012-01-08
  • 1970-01-01
  • 2012-08-31
  • 2012-05-11
  • 1970-01-01
  • 2014-04-02
  • 2012-12-20
相关资源
最近更新 更多