P
Pavel
Hello,
I need to implement a pair of regular STL-style 'find()' member
functions in a custom container, one for working on const containers and
another for non-const, along the same lines as these 2 in std::map:
iterator find(const key_type& x);
const_iterator find(const key_type& x) const;
The STL implementations I could see implement this by
copying-pasting-changing the function body; MSVC library by Dinkumware
uses inheritance of iterators (non-const one is derived from const) and
such but still duplicates too much code for my taste. Are there simpler
ways of writing the bulk of the function body only once for both const
and non-const instances? The algorithms for both are completely same:
the only difference is the const-ness of involved types.
I would really like to avoid the code duplication this time. I currently
use preprocessor to achieve this, but the code looks ugly in text editor
and I anticipate all types of issues with debugging, too, so I am
looking for more C++-ish technique.
Thanks,
Pavel
I need to implement a pair of regular STL-style 'find()' member
functions in a custom container, one for working on const containers and
another for non-const, along the same lines as these 2 in std::map:
iterator find(const key_type& x);
const_iterator find(const key_type& x) const;
The STL implementations I could see implement this by
copying-pasting-changing the function body; MSVC library by Dinkumware
uses inheritance of iterators (non-const one is derived from const) and
such but still duplicates too much code for my taste. Are there simpler
ways of writing the bulk of the function body only once for both const
and non-const instances? The algorithms for both are completely same:
the only difference is the const-ness of involved types.
I would really like to avoid the code duplication this time. I currently
use preprocessor to achieve this, but the code looks ugly in text editor
and I anticipate all types of issues with debugging, too, so I am
looking for more C++-ish technique.
Thanks,
Pavel