By vbo


2013-07-26 15:39:46 8 Comments

I'am trying to use std::unordered_set in cross-platform C++ application. It compiles and works like a charm in Visual C++ under Windows, but generates a fatal compilation error on clang under Mac OS X.

I want to know why it happens and what is the right way to get this working.

Example code:

// 
// 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 output:

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 Working implementation using @mfontanini's suggestion: https://gist.github.com/vbo/6090142.

1 comments

@mfontanini 2013-07-26 15:47:51

In order to make std::unordered_set work with your Point class, you can provide a std::hash specialization for it:

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

You could also change std::unordered_set's second template parameter(it defaults to std::hash<Point>), which indicates a functor type that returns the required hash.

It seems like you tried providing this hash implementation via a user defined conversion to size_t, but that won't work. The fact that it works in VC is caused by some bug in their implementation.

@mfontanini 2013-07-26 15:52:51

@vbo apparently, VC's implementation is bugged, making your user defined conversion work.

@Mike Seymour 2013-07-26 15:53:42

@vbo: Because Visual Studio is apparently doing something it shouldn't. (Most likely, defining a useless implementation of the primary hash template, or possibly using your conversion operator, but that's just a guess).

@vbo 2013-07-26 16:08:16

@mfontanini please add a sentence about strange Visual C++ behaviour to the answer itself. Not everyone reads comments.

@Tomilov Anatoliy 2015-08-11 06:00:46

I think it is superfluous to specialize std::hash. You can (or even must) just write your own user class for hash.

Related Questions

Sponsored Content

7 Answered Questions

[SOLVED] What is the difference between 'typedef' and 'using' in C++11?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2433985 View
  • 4242 Score
  • 1 Answer
  • Tags:   c++ c++-faq

15 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

25 Answered Questions

[SOLVED] What is the "-->" operator in C++?

40 Answered Questions

19 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 539798 View
  • 1863 Score
  • 19 Answer
  • Tags:   c++ linux profiling

9 Answered Questions

[SOLVED] What is a lambda expression in C++11?

4 Answered Questions

[SOLVED] What does T&& (double ampersand) mean in C++11?

2 Answered Questions

Sponsored Content