Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to cast a pair<Key, Value> to a pair<const Key, Value>?

So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair<Key, Value> in my iterator class (since I need to modify it), but at the same time I'd like the dereference functions to present a pair<const Key, Value> (actually it would be a const pair<const Key, Value>& and const pair<const Key, Value>* respectively). The only solution I've come up with so far is to dynamically allocate a new pair every time change the value that my iterator class points to changes. Needless to say, this is not a good solution.

I've also tried *const_cast<const pair<const Key, Value> >(&value) where value is declared as pair<Key, Value>.

Any help would be greatly appreciated (as would the knowledge that it can't be done).

EDIT

For the curious: I ended up storing a pair<const Key, Value> p in my iterator class. In order to change the pair I alter the two elements separately based on the underlying iterator (map<Key, Value>::const_iterator it), const_casting the key so that it could be altered, like this:

*const_cast<Key*>(&p.first) = it->first;
p.second = it->second;

Not a solution I'm terribly happy with, but it gets the job done, and the dereference methods are happy because I'm storing something of the correct type, which they can refer to.

like image 280
masaers Avatar asked Nov 29 '11 04:11

masaers


3 Answers

You can convert a value of type pair<Key,Value> to pair<const Key,Value>.

However, reading the question carefully, you're actually asking if, given a pair<Key,Value> you can create a pointer or reference to pair<const Key,Value> referring to the same object.

The answer is no - the only situation where a reference or pointer to one type can refer to an object of a different type is if the object type inherits from the referenced type.

One possibility is to return a pair of references, pair<const Key&, Value&>, created from the pair you wish to reference.

like image 200
Mike Seymour Avatar answered Nov 15 '22 02:11

Mike Seymour


Yes.

std::pair<int, double> p(1,2);
std::pair<const int, double> q = p;   // no problem

//q.first = 8;  // error
q.second = 9;

int b; double d;
std::pair<int &, double &> s(b,d);
std::pair<int const &, double &> t = s;  // also fine
like image 30
Kerrek SB Avatar answered Nov 15 '22 03:11

Kerrek SB


As Kerrek SB pointed out, you can construct std::pair<const Key, Value> from std::pair<Key, Value>. However, your original question implies that you want to avoid constructing std::pair objects each time your iterator is dereferenced.

Unfortunately, there is not a good way to do this. You may have to construct the pair object and actually store it somewhere, particular for operator->. Otherwise you have to be able to have your map actually store pair<const Key, Value> to be able to return references/pointers to it from your iterator. Basically to return a reference/pointer, it has to be stored somewhere in that form: it cannot be a temporary.

Avoid the const_cast. That's just asking for undefined behavior when you use it to cast pair this way even though it may work quite often.

like image 27
stinky472 Avatar answered Nov 15 '22 02:11

stinky472