Is it possible to get a type alias for size_type from a container such as std::vector or std::map without supplying a value_type of some kind?
I am writing a program where I need the size_type from two different map types. If, however, I create a typedef for the first map type, the name size_type is not available for the second. My workaround (a.k.a., "kludge") solution, it to tell a lie.
std::map<char, int> letters;
std::map<std::string, int> digraphs, trigraphs;
// This lie is good enough for both map types.
using size_type = typename std::map<int, int>::size_type;
In other cases, I would just prefer not to supply a value_type. Here is a contrived example.
// Yes, I know this can be cleaned up by doing it in stages, but ugh!
using size_type = typename std::map<std::string, std::pair<std::string, std::string>>::size_type;
At the end of the day, we all know size_type is probably going to be an alias for std::size_t, even in corner cases such as std::vector<bool>. Nevertheless, I still try to do things the "correct" way, by using size_type.
So, is there a way to get size_type without supplying value_type?
No, because
size_typecan be a different type for everyvalue_type.Of course, as you said yourself, that's normally not the case and
size_typeis almost always juststd::size_t, but both the standard library implementation itself, as well as user-defined specializations of the container templates, are allowed to definesize_typedifferently for differentvalue_typeand there is no requirement that it must be related tostd::size_t.There isn't even any requirement that
size_typemust be smaller or larger thanstd::size_t. If you want to have generic library code that works with any standard-conforming implementation, you'll have to always considersize_typeindividually and make no assumptions about their relation.