Returning NULL references...

T

Tony Di Croce

Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...

Does anyone know what does the STL does?

Tony
 
M

Moonlit

Hi,

Tony Di Croce said:
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...

Does anyone know what does the STL does?
Yes, it creates an entry to the map and returns a reference to it (that's
why [] isn't constant).

Do the same as the STL or throw an exception or return a reference to for
instance a local (to the class) variable.

Of course just adding an entry will keep yourself and people reading your
code, from any surprises since that is what people expect.

Regards, Ron AF Greve.
 
A

Andre Kostur

(e-mail address removed) (Tony Di Croce) wrote in
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

An exception. Or leave it as undefined behaviour.
This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...

Does anyone know what does the STL does?

Undefined behaviour. One of the strong points of references is that they
_cannot_ be "NULL" (at least not without invoking undefined behaviour).
 
P

Peter Koch Larsen

Tony Di Croce said:
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

Is it something like a std::vector? In that case, i would terminate the
program or possibly throw - but that is a design decision, of course.
This same question comes up virtually everywhere I return a reference.
I need to return a reference (as opposed to a copy) because I want to
use this operator as an l-value sometimes...

Sounds strange! Most of the time i return references, i will have a variable
to return.
Does anyone know what does the STL does?

For a set, the element is created with the value T(). For a std::vector,
behaviour is undefined - an implementation can do what it likes.

/Peter
 
A

Andre Kostur

(e-mail address removed) (Tony Di Croce) wrote in
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

An exception. Or leave it as undefined behaviour.
This same question comes up virtually everywhere I return a
reference. I need to return a reference (as opposed to a copy)
because I want to use this operator as an l-value sometimes...

Does anyone know what does the STL does?

Undefined behaviour. One of the strong points of references is that
they _cannot_ be "NULL" (at least not without invoking undefined
behaviour).

Oops.. I'm thinking specifically of vectors. For map and set that would
create a default-constructed object at that index and return a reference to
that....
 
M

Maitre Bart

Andre Kostur said:
(e-mail address removed) (Tony Di Croce) wrote in
Given:

T& operator [] ( int index ); // T is a parameterized type

What should be returned when index is out of range?

An exception. Or leave it as undefined behaviour.
This same question comes up virtually everywhere I return a
reference. I need to return a reference (as opposed to a copy)
because I want to use this operator as an l-value sometimes...

Does anyone know what does the STL does?

Undefined behaviour. One of the strong points of references is that
they _cannot_ be "NULL" (at least not without invoking undefined
behaviour).

Oops.. I'm thinking specifically of vectors. For map and set that would
create a default-constructed object at that index and return a reference to
that....

You may as well increase your vector size up to the specified index.
That way you would get about the same behavior you just described for map
and set.
Anyway, if the vector is not able to allocate, it will result in an
axception of some sort.
So better be prepared for an exception anyway.
 
A

Andre Kostur

Maitre Bart said:
reference

You may as well increase your vector size up to the specified index.

Up to what size? You may not know the maximum index before you start.
As a result, you'll have to pay for the construction of all of these
extra objects that you may not ever use....
That way you would get about the same behavior you just described for map
and set.

Not really. With map and set, you won't have a bunch of extra objects
being constructed just in case...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top