Why most implementations of pair don't use compression (empty base optimization) by default?

Viewed 516
2 Answers

Implementations of std::pair cannot use empty base optimization (EBO), as the objects it contains are the member subobjects, first and second, and EBO only applies to base class subobjects.

Note that implementations of std::tuple can use EBO, as its subobjects are exposed via std::get, which allows the use of base class subobjects.

Joseph Thomson answered the question.

I would like to add that if performance is important, std::pair and std::tuple are not your friends. These classes define constructors and assignments which make them non-trivial/non-POD classes even if they contain trivial/POD members, which precludes all optimizations for trivial/POD classes in the standard library.

If possible, instead define a struct with no constructors or assignments for the best performance. That also allows for human readable names. The rule of zero is your best performance friend.

Related