Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > out_of_range exception and subscript operator

Reply
Thread Tools

out_of_range exception and subscript operator

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      08-12-2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      08-12-2007
On 2007-08-12 15:47, , 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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      08-12-2007
, 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ée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
exception on string (out_of_range) eric C++ 8 07-07-2011 05:17 PM
How do I use std::out_of_range ? Steve555 C++ 3 04-29-2008 09:13 PM
Problem with std::out_of_range desktop C++ 17 05-23-2007 01:14 PM
why always out_of_range using size_type? Frederick Ding C++ 4 05-07-2005 02:43 PM
No out_of_range exception for "iterator + n" vs. vector.at( n ) Mike Austin C++ 13 10-21-2004 07:35 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57