Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do std::(unordered_)map and std::(unordered_)set share code?

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.

like image 577
Paul Avatar asked Oct 15 '25 00:10

Paul


1 Answers

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.

like image 99
Caleth Avatar answered Oct 17 '25 13:10

Caleth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!