Reducing the complexity of a template in C ++

Recently, I used one of the less used STL functions - custom allocators, and I need some serious help in reducing my semantic overhead. Take, for example, the definition of an unordered map that maps file names to an unordered map of int and shared_ptr pairs on a token, but uses a custom allocator.

typedef std::pair<int, int> token_key_type; typedef std::unordered_map< token_key_type, std::shared_ptr<Token>, std::hash<token_key_type>, std::equal_to<token_key_type>, Allocator< std::pair< const token_key_type, std::shared_ptr< Token > > > > filename_map_value_type; std::unordered_map< string, filename_map_value_type, std::hash<string>, std::equal_to<string>, Allocator< std::pair< const string, filename_map_value_type > > > tokens; 

These are 404 character definitions. And then, to build it, I have to pass a default value for each argument of the template, with the exception of Allocator, which cannot be configured by default, and counting the bucket for which there is no definition, resulting in another 168 characters just plotting the trait take it. Plus, of course, the same thing again every time I want to insert, because the value type of the first map also needs to be built.

Is there a way to avoid this without having to write my own unordered_map? This is seriously starting to slow down my performance.

Edit: Sorry! I meant, in general, for STL containers, and not just for unordered_map specifically, this is just the worst case. I also have this problem with a normal map, unordered_set, etc., And I cannot write a function to do all this for all possible STL containers that I might need using invidually.

+8
c ++ c ++ 11 visual-studio-2010
source share
2 answers

The icecrime solution can also only be performed a little more ugly for older compilers through the code below. You can also add factory functions to simplify the construction.

 template<typename K, typename V> struct unordered_map_type { typedef std::unordered_map< K, V, std::hash<K>, std::equal_to<K>, Allocator< std::pair<const K, V> > > type; }; typedef std::pair<int, int> token_key_type; typedef unordered_map_type<token_key_type, std::shared_ptr<Token> >::type filename_map_value_type; 
+6
source share

Unfortunately, I cannot provide complete and compiled code because I do not have a C ++ 0x compiler. However, I believe that C ++ 0x template aliases can be useful here:

 template<class Key, class Value> using custom_unordered_map = std::unordered_map < Key, Value, std::hash<Key>, std::equal_to<Value>, Allocator<std::pair<const Key, Value>> >; typedef custom_unordered_map<token_key_type, std::shared_ptr<Token>> filename_map_value_type; typedef custom_unordered_map<std::string, filename_map_value_type> your_typedef_name; 

Once again, sorry if this does not compile.

Also note that this was already possible in C ++ 03 using the optional 'indirect' type:

 template<class Key, class Value> struct custom_unordered_map { typedef std::unordered_map < Key, Value, std::hash<Key>, std::equal_to<Value>, Allocator<std::pair<const Key, Value> > > type; }; typedef custom_unordered_map<token_key_type, std::shared_ptr<Token> >::type filename_map_value_type; typedef custom_unordered_map<std::string, filename_map_value_type>::type your_typedef_name; 
+6
source share

Source: https://habr.com/ru/post/649832/


All Articles