This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: operator[] and std::map


Gilbert Laycock wrote:

> Should it be possible to use operator[] from std::map in a const
> context?
>
> For example, in class A, the isFive method will not compile (g++
> 3.2.3) because _m[c] does not preserve const. In contrast std::vector
> provides a const version of operator[].

No, there cannot be a const version of operator[] for std::map since, if
the key passed is not already present in the map, it will be added with
an associated value constructed using value_type's default constructor.

What you must do is use "find()" and, if the key is present in the map,
check the associated value.


Gawain

--
-------------------------------------------------------------------------------
Gawain Bolton                     | E-mail: boltong at nortelnetworks dot com
UMTS Development                  |
Nortel Networks                   | Voice:  ESN 579-3763   +33 1.39.44.37.63
Guyancourt, France                | FAX:    ESN 579-3009   +33 1.39.44.30.09
-------------------------------------------------------------------------------




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]