Re: Is there a proposal to fix the operator[] (rvalue/lvalue issues)??
"Gene Bushuyev" wrote:
The following standard conforming program would fail if the suggested changes
were implemented.
class A
{
mutable std::map<int,int> m;
public:
int f(int index) const { return m[index]; }
bool found(int index) const { return m.count(index); }
};
int main()
{
A a;
std::cout << a.f(0);
ASSERT(a.found(0));
}
Would it? Given that map is mutable wouldn't the non-const overload
take precedence even though the member function is const?
Actually, it is the above behaviour I am concerned about where the user
does not expect the element to have been added by calling a const
method. At present, it is the fault of the writer of the class who
knows that operator[] is a non-const operator and is choosing to call
it here knowing what will happen.
The more likely error to occur could happen when iterating through the
map. Now if your map is from K to shared_ptr<T> where K is a key-type,
and on iterating you call a method of T, woe to you if your map
suddenly contains empty shared_ptrs. You'll have your work cut out
working out where those came from.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]