From 6509750638859412349
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,c0e61b6a667823c0
X-Google-Attributes: gidf78e5,public
From: Bruce Visscher <bvisscher@mindspring.com>
Subject: Re: no const operator[] in STL map?
Date: 1999/01/25
Message-ID: <36ABD140.DAC0F0AA@mindspring.com>#1/1
X-Deja-AN: 436520948
Content-Transfer-Encoding: 7bit
Approved: Fergus Henderson <fjh@cs.mu.oz.au>
References: <916993358.766341@aloomba.aaii.oz.au> <slrn7ai4q5.9dj.sbnaran@fermi.ceg.uiuc.edu>
X-Original-Date: Sun, 24 Jan 1999 21:04:48 -0500
X-Accept-Language: en
X-Server-Date: 25 Jan 1999 02:00:44 GMT
Content-Type: text/plain; charset=us-ascii
X-Complaints-To: news@news.unimelb.edu.au
X-Trace: izvestia.its.unimelb.edu.au 917231670 9740 128.250.29.16 (25 Jan 1999 02:34:30 GMT)
Organization: MindSpring Enterprises
X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUANqvYHeEDnX0m9pzZAQFlJwGAjPBEIjJuS1C26xajkeQS3kNxrn6agG9f wVONWZMnrjymKahf8pZZ+snuEjzZCFbn =mvye
Mime-Version: 1.0
NNTP-Posting-Date: 25 Jan 1999 02:34:30 GMT
Newsgroups: comp.std.c++

Siemel Naran wrote:

> On 22 Jan 1999 15:51:17 GMT, Anthony Shipman <als@aaii.com.au> wrote:
>
> >The version of STL supplied for IRIX 6.5 does not have the
> >       const T& operator[](const key_type&) const
> >operator that is listed in the STL tutorial by Musser and Saini.
> >
> >Has it been removed from the standard or is it a fault with the SGI STL?
>
> No, the standard does not have such an operator.
> I wish it did.

Me too.

> [snip]
>
> But there is a logical alternative if const operator[] can't
> find the object with key_type 'key' in the map.  Just throw an
> exception.
>

I agree that it would have been nice had the standard provided a const
operator[].  However, I don't agree that it should throw.  To me there is an
obvious alternative: return by value rather than by reference and return
mapped_type() if the key is not found.

One useful application for map suggested in [Musser & Sauni] is that of a
sparse array.  With a const overload implemented like this you would get
consistent behavior (m[x] returns mapped_type() if x is not found whether or
not m is const).  In fact, the const overload would be the one to use if
wherever possible for a sparse vector.  You wouldn't want to allocate
10000000 elements if you did:

    struct T {/*...*/};
    map<int, T> const& g();
    void f(map<int, T> const&);
    const map<int, T>& m=g();
    for(int i=0; i<10000000; ++i)
        f(m);.

> There is already a precedent for such behaviour.

There is precedence for my suggestion too (typing the following in from my
pdf copy, I hope I don't make any mistakes):

26.3.2 Template class valarray
[...]
  template<class T>
  class valarray {
[...]
        // 26.3.2.3 element access:
        T            operator[](size_t) const;
        T&         operator[](size_t);
[...]

It's not that unusual in my experience to see non-const overload of
operator[] return by reference while the const overload returns by value.

This seems to bother some, though, so you could instead return by reference
to const and statically allocate a default constructed mapped_type when
needed instead (i.e., the first time map::operator[] const was invoked for a
non-existant key).

Bruce Visscher
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]



