Connecting Tech Pros Worldwide Help | Site Map

Const types in map

  #1  
Old August 19th, 2005, 07:55 PM
Garrett Kajmowicz
Guest
 
Posts: n/a
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
  #2  
Old August 19th, 2005, 09:25 PM
Esteban Osses
Guest
 
Posts: n/a

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

  #3  
Old August 20th, 2005, 02:25 PM
Garrett Kajmowicz
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
different types in the same map JonLT answers 4 July 25th, 2007 06:11 PM
How to make code accepting differet types work? Jim Langston answers 9 July 17th, 2006 03:05 PM
Storing my own key in map Tommo answers 4 November 17th, 2005 05:20 PM
Storing different types in container tuvok answers 17 July 23rd, 2005 06:16 AM