From 5388330350554857824 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,c0e61b6a667823c0 X-Google-Attributes: gidf78e5,public From: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran) Subject: Re: no const operator[] in STL map? Date: 1999/01/23 Message-ID: #1/1 X-Deja-AN: 436075326 X-NNTP-Posting-Host: fermi.ceg.uiuc.edu Approved: stephen.clamage@sun.com (comp.std.c++) References: <916993358.766341@aloomba.aaii.oz.au> X-UID: 0000000001 X-Status: $$$T Organization: University of Illinois at Urbana-Champaign Reply-To: sbnaran@KILL.uiuc.edu Newsgroups: comp.std.c++ Originator: clamage@taumet On 22 Jan 1999 15:51:17 GMT, Anthony Shipman 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. What happens if non-const operator[](const key_type& key) finds the object with key_type 'key' in the map? Then it just returns a non-const reference to the mapped_type. And what if it can't find the object in the map? It then creates an empty object (technically, it creates a std::pair with the mapped object initialized with the default constructor), and returns a reference to it. So the bottom line is that non-const operator[] always returns a non-const reference to a mapped_type. What happens if const operator[](const key_type& key) finds the object with key_type 'key' in the map? Then it just returns a const reference to the mapped_type. And what if it can't find the object in the map? It can't create an empty object as this changes the map, and a const function of class map should not change the map. So there's nothing to return. This is probably the rational for disallowing the function. 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. There is already a precedent for such behaviour. What happens if we dynamic_cast from Base& to Derived1&, and the object is really a Derived2&. As the value returned by dynamic_cast must be a Derived1&, we now have nothing to return. For pointer types, a failed dynamic_cast just returns NULL. But there is no such thing as a null reference. So dynamic_cast throws an exception. Similarly map's const operator[] could throw an exception if the object does not exist. Note furthermore that the bottom line above So the bottom line is that non-const operator[] always returns a non-const reference to a mapped_type is not correct. The process of creating the empty object might fail if there is no memory available. (In fact, the default constructor mapped_type::mapped_type() may throw any exception, but this is unusual). So the real bottom line is that non-const operator[] returns a non-const reference to a mapped_type or throws an exception. -- ---------------------------------- Siemel B. Naran (sbnaran@uiuc.edu) ---------------------------------- [ 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 ]