c++c++11stlstdmapsize-type

std::map::size_type for a std::map whose value_type is its own size_type


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?


Solution

  • What you are looking for is, generally speaking, impossible.

    It's conceivable (though far-fetched) that std::map<int, long>::size_type is int and std::map<int, int>::size_type is long (and similarly for other integer types), in which case there is no possible way to satisfy std::map<int, T>::size_type being T.

    Conversely, it could be that std::map<int, T>::size_type is defined as T for all T, in which case there is no unique T satisfying your "requirement".

    As mentioned by several answers (and your own reference link), in practice it's unlikely to be anything else than size_t.