Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I assign a std::pair that have one of it's component typed const?

Tags:

c++

std-pair

I'm trying to code an associative container compatible with std::map. To do so, I have to create an insert method that accept a new item in the form of an std::pair with the first component of a const type. For example : std::pair<const int, int> p.

The problem I have is that such an object can't be assigned to another. So in the inner code of my MapCompatibleContainer, I can't copy the new pair to the private variable (a std::vector).

How can I work around this?

Thanks

like image 417
Mathieu Pagé Avatar asked Mar 12 '11 21:03

Mathieu Pagé


1 Answers

As you say, you cannot assign to a const object.

The standard containers solve this by allocating raw memory and construct the object in place. Copy construction still works.

Also, the associative containers store each element in a separate memory block, so that they don't have to be copied later.

like image 166
Bo Persson Avatar answered Oct 23 '22 08:10

Bo Persson