C++ hash_map具有以下模板参数:
template<typename Key, typename T, typename HashCompare, typename Allocator>
Run Code Online (Sandbox Code Playgroud)
如何在不指定HashCompare的情况下指定分配器?
这不会编译:(
hash_map<EntityId, Entity*, , tbb::scalable_allocator>
Run Code Online (Sandbox Code Playgroud)
您可以使用一种技巧,它至少可以让您不必计算默认值,但它确实要求您知道hash_map.
hash_map 可能会被声明为:
class allocator {};
class hash_compare {};
template<typename Key
, typename T
, typename HashCompare = hash_compare
, typename Allocator = allocator>
class hash_map
{
public:
typedef HashCompare key_compare;
// ...
};
Run Code Online (Sandbox Code Playgroud)
我们不能省略散列的默认值,但我们可以使用成员 typedef 引用默认值:
hash_map<EntityId
, Entity*
, hash_map<EntityId,Entity*>::key_compare // find out the default hasher
, tbb::scalable_allocator> hm;
Run Code Online (Sandbox Code Playgroud)
如果您要大量使用该类型,请创建一个 typedef:
typedef hash_map<EntityId,Entity*>::key_compare EntityKeyCompare;
hash_map<EntityId
, Entity*
, EntityKeyCompare
, tbb::scalable_allocator> hm;
Run Code Online (Sandbox Code Playgroud)