I have a std::map<std::pair<std::string, std::string>, float> that is taking up too much memory, and in order to use less memory, I've decided to map the unique strings to integers (e.g., std::map<std::string, int>, where each new unique string is mapped to the current size() of the map), and use those integer value as pairwise keys to the map, (e.g., std::map<std::pair<int, int>, float>).
Instead of int, I want to use std::map::size_type:
using map_index = std::map::size_type;
std::pair<map_index, map_index> key;
Of course, this doesn't compile because I need to supply the argument list for the map:
vector.cc:14:19: error: invalid use of template-name `std::map' without an argument list
using map_index = std::map::size_type;
And this (in theory) is what I'm trying to achieve:
using map_index = std::map<std::string, map_index>::size_type;
which gives the following (expected) compiler error:
vector.cc:15:41: error: `map_index' was not declared in this scope
using map_index = std::map<std::string, map_index>::size_type;
What is the proper way to get the compiler to infer the correct value_type for a std::map whose value_type is its own size_type?
size_t should be good enough for such case.
But if you insist, you can do like this:
#include <type_traits>
#include <map>
template <class Key, class Value = size_t, size_t depth = 0, class = void>
struct GetSizeType {
using type = typename GetSizeType<Key, typename std::map<Key, Value>::size_type, depth + 1>::type;
};
template <class Key, class Value, size_t depth>
struct GetSizeType<Key, Value, depth, std::enable_if_t<std::is_same_v<Value, typename std::map<Key, Value>::size_type>>> {
using type = typename std::map<Key, Value>::size_type;
};
template <class Key, class Value>
struct GetSizeType<Key, Value, 100, void> {};
int main() {
using X = GetSizeType<int>::type;
return 0;
}
It will run recursively on GetSizeType, the recursive call will stop upon
type in this case), orstd::map of which the mapped_type and size_type is identical (the member type aliases size_type).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With