9

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.

masaers
  • 697
  • 9
  • 21
  • 5
    It's hard to believe you would go to such great lengths of effort to post this little essay without even **trying**: `std::pair p(1,2); std::pair q = p; // fine` – Kerrek SB Nov 29 '11 at 04:24
  • 3
    @Kerrek SB: I think you overlooked an important part of Op's question. He wants to avoid constructing a new pair object each time his custom iterator's dereference/member selection operator is called. That becomes especially important in operator-> as it would mean his iterator would have to potentially store a member pair and copy to it each time the function is called or when the iterator is incremented/decremented. – stinky472 Nov 29 '11 at 04:39
  • @KerrekSB: Thank you for teaching me a lesson in question asking. I will strive to express myself clearer in the future. – masaers Nov 29 '11 at 04:47
  • 1
    I was curious how `std::map` did it, since it has `extract()`. With Apple's Clang STL, the node holds a `std::pair`. The node has a `__ref()` which returns a `std::pair` which it produces by `const_cast`ing the key. While https://en.cppreference.com/w/cpp/container/node_handle says `node_handle` is UB "if a user-defined specialization of std::pair exists for std::pair or std::pair", (making room for casting the whole pair, I suspect), it seems like my implementation avoids this. – Ben Jun 04 '21 at 13:25

5 Answers5

10

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.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Thank you, I'll see if the pair-of-references solution will solve my particular problem (and thank you for the careful reading, I'll try to express myself more clearly in the future). – masaers Nov 29 '11 at 04:42
5

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
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • In my particular case, this would lead to a case of returning a reference to a temporary, which fails to compile. – masaers Nov 29 '11 at 04:44
  • 1
    @MarkusSaers: Given your `pair p`, you can return a `pair(p.first, p.second)`, which won't create any temporaries. – Kerrek SB Nov 29 '11 at 04:55
2

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.

stinky472
  • 6,737
  • 28
  • 27
0

I met exactly the same problem. My solution was to create a new map object as part of the iterator class, then add to it members that are missed in the upstream map class and return the reference to members of it. Not very efficient, but works.

Your solution have two issues:

  1. Assigning a const variable with const_cast is undefined behavior. The compiler optimisations could provide strange results.
  2. Any new dereference would invalidate the results of previous dereference. It should not to. So, depending of the usage of your iterator it may also produce strange results.
Denis
  • 1
  • But in my case, there is no upstream map object (it is a custom container class, that do not contain std::pair elements that can be referred to. – masaers Oct 29 '15 at 01:07
  • Still, you can create internal temporary std::pair elements on every iterator dereference one way or another, and destroy them when the iterator instance destroys. – Denis Jan 12 '16 at 10:15
0

Corrected by @antonpp, this is an undefined behaviour that shouldn't be recommended any more.


This can be solved by reinterpret_cast, and it is purely a compile-time directive.

std::pair<int, double> p1 = { 1, 2.3 };
auto& p2 = reinterpret_cast<std::pair<const int, double>&>(p1);    // type of p2 is pair<const int, double>&

//++(p2.first);    // error: expression must be a modifiable lvalue
++(p1.first);
assert(p2.first == 2);


However, reinterpret_cast is a dangerous behaviour. I would suggest adding an static assert, to prevent unexpected partial template specialization.

using srcType = std::pair<Key, Value>;
using tarType = std::pair<const Key, Value>;

static_assert(
    offsetof(srcType, first) == offsetof(tarType, first)
 && offsetof(srcType, second) == offsetof(tarType, second)
);
zjyhjqs
  • 609
  • 4
  • 11
  • 1
    This is undefined behaviour either way even if all types have standard layout. This is specifically called out in https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing (example with std::pair). – antonpp Mar 28 '22 at 18:48