Something about std::list<> member functions.
Gabriel Dos Reis
gdr@integrable-solutions.net
Sat Aug 9 17:03:00 GMT 2003
Gawain Bolton <gbolton@free.fr> writes:
| Dhruv Matani wrote:
|
| >I was wondering why in std::list<>, the functions merge, unique, sort,
| >... are implemented twice, once for the less than operator, and once for
| >the template argument one. Why can't it be coded just once for the more
| >general version, and pass std::less<> to then more general one, when the
| >less general one is called?
| >
| Good question! But this kind of thing is not limited to the
| std::list class, the generic algorithms in stl_algobase.h and
| stl_algo.h also currently have duplicated code for functions with and
| without comparison objects or predicate objects as they're sometimes
| called.
|
| Unless there is a good reason for not doing this, I would be happy to
| submit a patch for std::list.
Nearly a year ago -- at the last Santa Cruz meeting -- Benjamin and I
elaborated on that issue. Basically
1) I introduced function objects (implemented in our internal
namespace) for the usual comparisons
2) I made the less specific functions forward to most general ones
by adding an instance of the function objects.
That substantially reduced the duplications you find here and there.
Then, I discovered that our lovely compiler is not always inlining
those pretty trivial function objects. Then I didn't apply the
patches.
So the only reason for the current situation is GCC not always honouring
inline request in those simple situations. Code duplication removal
in this case will not be interesting if it means lees efficient library.
-- Gaby
More information about the Libstdc++
mailing list