Connecting Tech Pros Worldwide Help | Site Map

Const types in map

Garrett Kajmowicz
Guest
 
Posts: n/a
#1: Aug 19 '05
Is the following legal:

std::map<int, const int> q;

In short, does the const int violate the Container requirements in 23.1.3
that the types be assignable?

Thanks.

- Garrett
Esteban Osses
Guest
 
Posts: n/a
#2: Aug 19 '05

re: Const types in map


I'd say it's legal.
The type is assignable, however there is a constraint on using the
assignment operator (and any other method/operator modifying its
content).
To insert a new item in such a map you can always do:

q.insert( std::pair<int, const int>(3, 5) );

but
q[ 3 ] = 5;

is not allowed

--
Esteban

Garrett Kajmowicz
Guest
 
Posts: n/a
#3: Aug 20 '05

re: Const types in map


[color=blue]
> I'd say it's legal.
> The type is assignable, however there is a constraint on using the
> assignment operator (and any other method/operator modifying its
> content).
> To insert a new item in such a map you can always do:
>
> q.insert( std::pair<int, const int>(3, 5) );
>
> but
> q[ 3 ] = 5;
>
> is not allowed[/color]

My understanding is that 23.1.4 (Table 2) has the following requirements:

Table 2--Assignable requirements

+------------------------------------------------+
|expression return type post-condition |
+------------------------------------------------+
|t = u T& t is equivalent to u |
+------------------------------------------------+

How does that make a const type assignable for the purposes of map?

- Garrett
Closed Thread