This code compiles fine with g++ 4.4 and ‘-std=c++0x’.
#include <unordered_set>
namespace
{
size_t IntHash ( int i )
{
return i;
}
bool IntComp ( int i, int j )
{
return i == j;
}
}
int main ( int argc, char* argv[] )
{
typedef std::pointer_to_unary_function<int, size_t> CustomHash;
typedef std::pointer_to_binary_function<int, int, bool>
CustomComp;
typedef std::unordered_set<int, CustomHash, CustomComp> DeprecatedSet;
DeprecatedSet deprecatedSet ( 10, std::ptr_fun ( IntHash ), std::ptr_fun ( IntComp ) );
deprecatedSet.insert ( 5 );
deprecatedSet.insert ( 10 );
}
Say, however, I didn’t want to use the deprecated std::pointer_to_unary_function and std::ptr_fun, but still use the free functions:
#include <unordered_set>
#include <functional>
namespace
{
size_t IntHash ( int i )
{
return i;
}
bool IntComp ( int i, int j )
{
return i == j;
}
}
int main ( int argc, char* argv[] )
{
typedef std::unordered_set<int /*, UserDefinedHash?, UserDefinedComparison? */> NewSet;
NewSet newSet (
10,
std::bind ( IntHash, std::placeholders::_1 ),
std::bind ( IntComp, std::placeholders::_1, std::placeholders::_2 ) );
newSet.insert ( 5 );
newSet.insert ( 10 );
}
This doesn’t compile, I assume because I’m not sure what to put in for UserDefinedHash and UserDefinedComparison.
It doesn’t look like std::bind has a member type defining the type of the bind object itself.
I’m aware that there’s other ways to define custom hash functions and comparisons, just curious if free/class functions can be used without deprecated standard library types and functions.
You can use:
And in order to use
std::bind, you can usedecltypeorstd::function