| View previous topic :: View next topic |
| Author |
Message |
Tony Di Croce Guest
|
Posted: Wed Sep 29, 2004 3:56 pm Post subject: Returning NULL references... |
|
|
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
|
|
| Back to top |
|
 |
Moonlit Guest
|
Posted: Wed Sep 29, 2004 4:02 pm Post subject: Re: Returning NULL references... |
|
|
Hi,
"Tony Di Croce" <dicroce (AT) gmail (DOT) com> wrote
| Quote: | 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.
|
|
| Back to top |
|
 |
Andre Kostur Guest
|
Posted: Wed Sep 29, 2004 4:03 pm Post subject: Re: Returning NULL references... |
|
|
[email]dicroce (AT) gmail (DOT) com[/email] (Tony Di Croce) wrote in
news:37c0928e.0409290756.721f58d8 (AT) posting (DOT) google.com:
| Quote: | 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.
| Quote: | 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).
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Wed Sep 29, 2004 4:04 pm Post subject: Re: Returning NULL references... |
|
|
"Tony Di Croce" <dicroce (AT) gmail (DOT) com> skrev i en meddelelse
news:37c0928e.0409290756.721f58d8 (AT) posting (DOT) google.com...
| Quote: | 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.
| Quote: |
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.
| Quote: |
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
|
|
| Back to top |
|
 |
Andre Kostur Guest
|
Posted: Wed Sep 29, 2004 4:12 pm Post subject: Re: Returning NULL references... |
|
|
Andre Kostur <nntpspam (AT) kostur (DOT) net> wrote in
news:Xns95735C19ECCECnntpspamkosturnet (AT) 207 (DOT) 35.177.134:
| Quote: | dicroce (AT) gmail (DOT) com (Tony Di Croce) wrote in
news:37c0928e.0409290756.721f58d8 (AT) posting (DOT) google.com:
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....
|
|
| Back to top |
|
 |
Maitre Bart Guest
|
Posted: Wed Sep 29, 2004 5:53 pm Post subject: Re: Returning NULL references... |
|
|
"Andre Kostur" <nntpspam (AT) kostur (DOT) net> wrote
| Quote: | Andre Kostur <nntpspam (AT) kostur (DOT) net> wrote in
news:Xns95735C19ECCECnntpspamkosturnet (AT) 207 (DOT) 35.177.134:
[email]dicroce (AT) gmail (DOT) com[/email] (Tony Di Croce) wrote in
news:37c0928e.0409290756.721f58d8 (AT) posting (DOT) google.com:
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.
|
|
| Back to top |
|
 |
Andre Kostur Guest
|
Posted: Wed Sep 29, 2004 7:08 pm Post subject: Re: Returning NULL references... |
|
|
"Maitre Bart" <lemieux (AT) cae (DOT) com> wrote in news:cjesqn$9ac$1 (AT) dns3 (DOT) cae.ca:
| Quote: | 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.
|
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....
| Quote: | 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...
| Quote: | 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.
|
|
|
| Back to top |
|
 |
|