Looking for a way to avoid copy-n-pasting

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
 
B

Bart van Ingen Schenau

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

The possible solutions depend entirely on the conversions that are
possible between iterator and const_iterator.
If there are no conversions between the two possible, you are stuck
with the preprocessor solution.

If you can convert a const_iterator to an iterator, then you can
implement the non-const version as:

iterator find(const key_type& x)
{
const_iterator temp = const_cast<const Cont*>(this)->find(x);
return iterator(temp);
}

If you can convert an iterator to a const_iterator, you can use the
same trick as above to forward the call, but you will need to put a
big warning sign in the code that the non-const find must not be
changed to modify *this.

Bart v Ingen Schenau
 
P

Pavel

Bart said:
The possible solutions depend entirely on the conversions that are
possible between iterator and const_iterator.
If there are no conversions between the two possible, you are stuck
with the preprocessor solution.

If you can convert a const_iterator to an iterator, then you can
implement the non-const version as:

iterator find(const key_type& x)
{
const_iterator temp = const_cast<const Cont*>(this)->find(x);
return iterator(temp);
}

If you can convert an iterator to a const_iterator, you can use the
same trick as above to forward the call, but you will need to put a
big warning sign in the code that the non-const find must not be
changed to modify *this.

Bart v Ingen Schenau

Thanks Bart!


I did not want const_iterator->iterator conversion for obvious reasons
(then someone will be able to start changing const container using the
iterator received with some const_iterator begin() const; method or
similar).

I did not want a reverse conversion either because then I would have to
do const_cast from const to non_const for the container reference and
that did not look safe to me due to potential impossibility of such consts.

You are right that then I have to generate 2 versions, I just wanted to
do it in some C++-ish way. After some thought, I came to this compromise
below. It looks kinda ugly to me and it's way more sophisticated than I
would love to have it but seems working. The idea is to delegate actual
work to a static private member function template. I am sure there are
exploits of this so wanted to have a second opinion. Below is the
working example (the container there is just a toy, of course, not a
real thing) -- please feel free to comment:

(also tried to rework GetIterType to deal with const/non-const
references template parameters instead of pointers, just for aesthetic
reasons, and failed miserably. Any ideas if this is possible to do at
all? I thought I was able to strip references in partial specialization
in the past, but this time all compilers are against me..)

---------------cut here -----------------
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

template<class Cont, typename ContPtr> struct GetIterType;

template<class Cont>
struct GetIterType<Cont, const Cont *>
{
typedef typename Cont::const_iterator Result;
};

template<class Cont>
struct GetIterType<Cont, Cont *> {
typedef typename Cont::iterator Result;
};

template<typename T>
class Container
{
typedef vector<T> BaseCont_;

typedef Container<T> Self_;

template<typename ContPtr>
typename GetIterType<Self_, ContPtr>::Result
static find_(ContPtr c, const T &t) {
return std::find(c->data.begin(), c->data.end(), t);
}

public:
BaseCont_ data;
typedef typename BaseCont_::iterator iterator;
typedef typename BaseCont_::const_iterator const_iterator;

const_iterator find(const T& t) const { return find_(this, t); }
iterator find(const T& t) { return find_(this, t); }
};

int main() {
Container<int> c;
c.data.push_back(5);
Container<int>::iterator i = c.find(5);
cout << "*i=" << *i << endl;
const Container<int> cc = c;
Container<int>::const_iterator ci = cc.find(5);
cout << " *ci=" << *ci << endl;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top