B
brutus
Hi,
I'm trying to define a map-like interface to a container.
My basic problem is that the container is associative,
but values are not stored in pairs internally.
(and as we know, the map value_type must be pair<const Key, T>)
Instead there's a list (vector) of values, and a method
to get key reference.
But I'm ready to increase the values vector to store pairs.
So... Internally, values are actually stored in a vector.
But std::vector can only store pair with non-const types.
Now I have
std::vector<std:air<Key, T> > m_values;
and
typedef std:air<const Key, T> value_type;
How do I define the reference type returned from (non-const) iterators?
Trouble invites itself to the iterator dereference operator:
class iterator {
friend class MyContainer;
iterator(const ValueList* values) : m_values(values), current(0) {}
public:
reference operator*() const { ...? }
};
Now, in boost concept checks for Forward Container (which is
required for Pair Associative and most any container types),
I get back-paddled in
reference r = *it;
I see two ways of getting past this:
(i) typedef value_type reference;
and returning a copy of the value - but this has wrong semantics:
assigning to (*iter).second doesn't change the stored value.
(ii) return *reinterpret_cast<value_type*>(&(*m_vector)[current]);
Well, it works, sort of, but aint pretty... or portable?
Any better approach?
Should I define a custom value_type class with desired behaviour?
And if so, doyouknow some similar example I could look at?
(I've no desire to attempt to write a custom vector class that stores
pairs with const members, but perhaps one exists somewhere?)
(Of course I could make it look like anything, ie let iterator point to
mapped_type and add a method key() to access the key, or whatever,
but while it's an option, it's not the point...)
Thanks,
homsan
I'm trying to define a map-like interface to a container.
My basic problem is that the container is associative,
but values are not stored in pairs internally.
(and as we know, the map value_type must be pair<const Key, T>)
Instead there's a list (vector) of values, and a method
to get key reference.
But I'm ready to increase the values vector to store pairs.
So... Internally, values are actually stored in a vector.
But std::vector can only store pair with non-const types.
Now I have
std::vector<std:air<Key, T> > m_values;
and
typedef std:air<const Key, T> value_type;
How do I define the reference type returned from (non-const) iterators?
Trouble invites itself to the iterator dereference operator:
class iterator {
friend class MyContainer;
iterator(const ValueList* values) : m_values(values), current(0) {}
public:
reference operator*() const { ...? }
};
Now, in boost concept checks for Forward Container (which is
required for Pair Associative and most any container types),
I get back-paddled in
reference r = *it;
I see two ways of getting past this:
(i) typedef value_type reference;
and returning a copy of the value - but this has wrong semantics:
assigning to (*iter).second doesn't change the stored value.
(ii) return *reinterpret_cast<value_type*>(&(*m_vector)[current]);
Well, it works, sort of, but aint pretty... or portable?
Any better approach?
Should I define a custom value_type class with desired behaviour?
And if so, doyouknow some similar example I could look at?
(I've no desire to attempt to write a custom vector class that stores
pairs with const members, but perhaps one exists somewhere?)
(Of course I could make it look like anything, ie let iterator point to
mapped_type and add a method key() to access the key, or whatever,
but while it's an option, it's not the point...)
Thanks,
homsan