Boost logo

Boost Users :

Subject: Re: [Boost-users] TR1 unordered_set: compilation failure with initialization
From: Aman Sinha (aman.sinha_at_[hidden])
Date: 2010-02-27 18:42:46


I want to pass in my own hash functor and comparator to an unordered_set initialization. It fails compilation on g++ 4.2.1, boost version 1.42. It's not clear why, since a similar initialization of an STL set container seems to work fine... see below for the code and compilation failure. Any insight would be highly appreciated.

#include <set>
#include <boost/tr1/unordered_set.hpp>

class foo_compare {
public:
    foo_compare(bool is_gt) : m_is_gt(is_gt) {}
    bool operator() (int a, int b)
    {
        if (m_is_gt)
            return a > b;
        else
            return a < b;
    }

    bool m_is_gt;
};

class foo_hash1
{
public:
    foo_hash1() {}
    ~foo_hash1() {}
    size_t operator() (int val) const
    {
        return val % 19;
    }
};

using namespace std;
typedef set<int, foo_compare> MySetT;
typedef std::tr1::unordered_set<int, foo_hash1, foo_compare> MyUnorderedSetT;

size_t hash_func_1(int val) {
    return val % 19;
}

class Foo {
public:
    Foo(bool is_gt) :
        m_set(foo_compare(is_gt)), // this initialization is OK
        m_unordered_set(foo_hash1() , // this one fails compilation! why ?
                        foo_compare(is_gt))
    {}

    MySetT m_set;
    MyUnorderedSetT m_unordered_set;

};

int main()
{
    bool is_greater_than = true;
    Foo myfoo(is_greater_than);

    return 0;
}

g++ my_unordered.cpp -I ~/temp/boost_1_42_0
my_unordered.cpp: In constructor Foo::Foo(bool)â:
my_unordered.cpp:43: error: no matching function for call to âboost::unordered_set<int, foo_hash1, foo_compare, std::allocator<int> >::unordered_set(foo_hash1, foo_compare)â
/home/asinha/temp/boost_1_42_0/boost/unordered/unordered_set.hpp:175: note: candidates are: boost::unordered_set<T, H, P, A>::unordered_set(boost::unordered_detail::move_from<boost::unordered_set<T, H, P, A> >) [with T = int, H = foo_hash1, P = foo_compare, A = std::allocator<int>]
/home/asinha/temp/boost_1_42_0/boost/unordered/unordered_set.hpp:121: note: boost::unordered_set<T, H, P, A>::unordered_set(const boost::unordered_set<T, H, P, A>&, const A&) [with T = int, H = foo_hash1, P = foo_compare, A = std::allocator<int>]
/home/asinha/temp/boost_1_42_0/boost/unordered/unordered_set.hpp:115: note: boost::unordered_set<T, H, P, A>::unordered_set(const A&) [with T = int, H = foo_hash1, P = foo_compare, A = std::allocator<int>]
/home/asinha/temp/boost_1_42_0/boost/unordered/unordered_set.hpp:110: note: boost::unordered_set<T, H, P, A>::unordered_set(size_t, const H&, const P&, const A&) [with T = int, H = foo_hash1, P = foo_compare, A = std::allocator<int>]
/home/asinha/temp/boost_1_42_0/boost/unordered/unordered_set.hpp:43: note: boost::unordered_set<int, foo_hash1, foo_compare, std::allocator<int> >::unordered_set(const boost::unordered_set<int, foo_hash1, foo_compare, std::allocator<int> >&)

Aman Sinha



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net