Re: std::vector::operator[] throws exception
James Dennett wrote:
Deane Yang wrote:
{ for questions specific to VC++ please consider posting
to 'microsoft.public.vc.language' newsgroup. -mod }
James Kanze wrote:
Deane Yang wrote:
> I was surprised to learn recently that code like
> std::vector<int> v(3);
> v[3] = 10;
> compiled with VC++ 8.0 and the STL library that comes with it
> throws an exception when run.
> Does this conform to the official specification of std::vector?
> No exception is thrown if compiled with cygwin gcc.
I get a core dump with g++ under Linux or Solaris. At least
with debug options turned on.
First, I misspoke, and this is not so different from VC++ does. To be
precise, VC++ invokes an "invalid parameter handler", which by default
terminates the program. There is no exception thrown.
But what is different is that VC++ does this even with programs compiled
in release mode. Can someone explain to me why this is desirable default
behavior?
I'd like to turn this around and ask how you would prefer
this programming error (accessing out of bounds) to be
handled? Program termination seems a reasonable choice
to me in many circumstances.
The short answer is that I'd like the programmer to be able to decide,
and I thought the choice between operator[] and the member function
"at" was supposed to provide that choice.
I agree that program termination is not just a reasonable but the
*only* choice in many *but not all* circumstances. I write mostly
numerical code delivered to clients as libraries, where an out of
bounds access error is consequence of a programming error analogous to
division by zero. An assert in debug mode is desirable, but I don't
want my clients' programs to blow up when using my library in release
mode.
I am not experienced enough to know whether my views are reasonable or
not, so if I am misguided, I would be grateful if someone could provide
a patient explanation of why.
Deane
{ please do not quote the banner like you did below. a new one is always
appended to all posts automatically by the server. thanks! -mod }
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]