473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

Aug 12 '07 #1
2 2296
On 2007-08-12 15:47, su**************@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
Aug 12 '07 #2
su**************@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ée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Steve | last post by:
Hi, I hope someone can help. I have a class called cField, and another class called cFieldList. cFieldList contains a std::vector of cFields called myvec I've overloaded the subscript...
5
by: Steve | last post by:
Hi, I have a class called cList as so: template<class T> class cList { // base class for Lists private: protected: vector<T> tListOf; // field list container public: void Add(const T& t)...
13
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not...
4
by: Frederick Ding | last post by:
Hi,guys! Please look at this problem, when I use "int i", it success, but when I use string::size_type i, it fail with a out_of_range exception. //----------------...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
2
by: Steve555 | last post by:
Hi, In a function that erases part of a string, the compiler sometimes gives this error: terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::erase I...
17
by: desktop | last post by:
I have the following code: #include <stdexcept> int main(){ int i = 10; int arr; int index = 11;
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.