Let's say that I care about binary size, I already use std::map
, and I need a set. Instead of using std::set<T>
, I could use std::map<T, bool>
. Will that help, or is common code already used under the hood?
I doubt that the C++ standard says something about sharing code, so it might vary between implementations, but I assume there's some common practice.
This is a thing that you will have to measure and see. You are right that std::map<T, std::monostate>
is more similar to std::map<T, U>
than std::set<T>
is, as the underlying comparisons have to go via value.first
, rather than being directly value
.
Afaik, the main implementations do implement all the associative containers with one base class template, and similarly all the unordered associative containers with one class template. However, these are all templates, so there will be differing amounts of object code re-use arising from that.