Const types in map

G

Garrett Kajmowicz

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
 
E

Esteban Osses

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
 
G

Garrett Kajmowicz

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top