Possible Duplicate:
Why does stack<const string> not compile in g++?
An answer to another question
explained why we (supposedly) can’t have containers of const objects. For example, this is not allowed:
vector<const int> v; //not allowed
But why does a pair allow the first object to be const? This is, indeed, what happens with the pairs inside a map object. Am I missing something?
Detailed and intuitive explanations of this phenomenon would be greatly appreciated.
I think the main reason why is because
std::pairdoes not reallocate objects, so they don’t have to be assignable.Update:
Actually vector is the only container that requires assignable objects. This is because accordingly to the standard vector must have a contiguous storage location for it’s elements. So if there will be no room for more objects to add, vector will have to reallocate it’s data to another place (thus using the assignable property of the objects).