out_of_range exception and subscript operator



 DEVELOP > c-Plus-Plus > out_of_range exception and subscript operator

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "India"
Date: 12 Aug 2007 08:47:47 AM
Object: out_of_range exception and subscript operator
For vector and deque, the 'at( )' member function throws out_of_range
exception if the argument to the 'at( )' function is not in range. But
the subscript operator [ ] does not throw this exception for the same
situation.
My question:
Why can't the subscript operator [ ] itself throw the out_of_range
exception in which case the 'at ( )' member function may not be
needed ?
Kindly explain
Thanks
V.Subramanian
.

User: "=?ISO-8859-1?Q?Erik_Wikstr=F6m?="

Title: Re: out_of_range exception and subscript operator 12 Aug 2007 08:56:58 AM
On 2007-08-12 15:47, subramanian100in@yahoo.com, India wrote:

For vector and deque, the 'at( )' member function throws out_of_range
exception if the argument to the 'at( )' function is not in range. But
the subscript operator [ ] does not throw this exception for the same
situation.

My question:
Why can't the subscript operator [ ] itself throw the out_of_range
exception in which case the 'at ( )' member function may not be
needed ?

Because sometimes you can be sure that the index provided will be in
range and don't want the extra overhead associated with the check, the
operator[] is meant for occasions where you know that the code is safe
and you want as much performance as possible.
--
Erik Wikström
.

User: "James Kanze"

Title: Re: out_of_range exception and subscript operator 12 Aug 2007 10:56:22 AM
subramanian100in@yahoo.com, India wrote:

For vector and deque, the 'at( )' member function throws out_of_range
exception if the argument to the 'at( )' function is not in range. But
the subscript operator [ ] does not throw this exception for the same
situation.
My question:
Why can't the subscript operator [ ] itself throw the out_of_range
exception in which case the 'at ( )' member function may not be
needed ?

Because that's usually not what you want. You want some sort of
implementation defined crash, the equivalent of an assertion
failure. Undefined behavior allows the implementation to
provide this (and also allows removing the checks, if you find
that they create too much runtime overhead).
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER