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]

operator[] and std::map


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[].


#include <map>

class A {
public:
  A () : _m() {}

  void set (char c, int v) {
    _m[c] = v;
  }

  bool isFive (char c) const {
    return  _m[c] == 5; 
  }

private:
  std::map<char, int> _m;
};


-- 

  Gilbert Laycock                 email:          gtl1 at mcs dot le dot ac dot uk
  Maths and Computer Science,     http://www.mcs.le.ac.uk/~glaycock
  University of Leicester         phone:         (+44) 116 252 3902
  UK                              GPG:                     7D025C00


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