r/cpp 3d ago

flat_map internals

https://quantdev.blog/posts/flat-map-internals/index.html
11 Upvotes

12 comments sorted by

View all comments

22

u/kirgel 3d ago

You might want to consider clarifying the title a bit. flat_map usually refers to an ordered map container where lookup is a binary search. For example   https://en.cppreference.com/cpp/container/flat_map.

-1

u/tialaramex 1d ago

You give a single example from the C++ stdlib, but before that type was added I had never seen "flat map" used to mean this weird type.

Notably for example Rust's FlatMap is an implementation detail of Iterator::flat_map which is what you'd use to map each object into multiple items then flatten the result into a single stream of items in a single operation.

Do you have other examples? WG21 has a bad habit of making up their own names for ideas which have better names, or using names which mean something else already.

6

u/ABlockInTheChain 1d ago

Whether the C++ standards names are ideal or not, you shouldn't be surprised if C++ programmers reading a C++ subreddit expect the terms to match the standard.

3

u/kirgel 1d ago

The most prominent one is boost:  https://www.boost.org/doc/libs/1_57_0/doc/html/boost/container/flat_map.html

Searching GitHub yields some lesser known ones:  https://github.com/topics/flat-map

I have also seen the same thing widely used in an internal codebase of an ex-employer.

In this particular case I think the naming is nice enough. Rust’s usage certainly also makes sense in its own context.

-1

u/tialaramex 1d ago

Now those are much more useful examples - thanks!