Skipping C ++ Template Parameter

C ++ hash_map has the following template options:

template<typename Key, typename T, typename HashCompare, typename Allocator> 

How can I specify Allocator without specifying HashCompare?

This will not compile :(

 hash_map<EntityId, Entity*, , tbb::scalable_allocator> 
+4
source share
3 answers

There is one trick you can use that will at least keep you working by default, but this requires that you know the type name as defined in hash_map .

The hash_map file is likely to be declared as:

 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; // ... }; 

We cannot leave the default value for the hash, but we can refer to the default value using the typedef member:

 hash_map<EntityId , Entity* , hash_map<EntityId,Entity*>::key_compare // find out the default hasher , tbb::scalable_allocator> hm; 

If you are going to use this type a lot, then create a typedef:

 typedef hash_map<EntityId,Entity*>::key_compare EntityKeyCompare; hash_map<EntityId , Entity* , EntityKeyCompare , tbb::scalable_allocator> hm; 
+3
source

The simple answer is that you cannot. You cannot skip the template parameter and select the default value for the template. Your only option is to find out what the default is and paste it into your declaration.

+11
source

If the map type has some public typedef for the HashCompare template HashCompare , you can write a meta function that uses the vanilla hash map type to get the std comparator. Something like that:

 template< typename Key, typename T, typename Allocator> struct hash_map_type { typedef typename hash_map<Key,T>::key_compare key_compare; typedef mash_map<Key,T,key_compare,Allocator> result_t; }; typedef hash_map_type<int,string,my_allocator>::result_type my_hash_map; 

This, however, depends on what is available above hash_map<Key,T>::key_compare .

+3
source

All Articles