To help balance out "introductory," "intermediate," and "advanced" content, we're trying an experiment to highlight interesting bite-sized tidbits that are in the first two categories.
Here's today's tidbit from StackOverflow's [c++11] tag:
The declaration for the
[]
operator on astd::map
is this:T& operator[] ( const key_type& x );Is there a reason it isn't this?
T& operator[] ( const key_type& x ); const T& operator[] const ( const key_type& x );Because that would be incredibly useful any time you need to access a member map in a
const
method.
As the two top answers show, the answer is different in C++98 and C++11, and C++11 is where it's "at" (pardon).
Add a Comment
Comments are closed.