Returning NULL references...

Discussion in 'C++' started by Tony Di Croce, Sep 29, 2004.

  1. 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
     
    Tony Di Croce, Sep 29, 2004
    #1
    1. Advertising

  2. Tony Di Croce

    Moonlit Guest

    Hi,

    "Tony Di Croce" <> wrote in message
    news:...
    > 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).

    >
    > Tony


    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.
     
    Moonlit, Sep 29, 2004
    #2
    1. Advertising

  3. Tony Di Croce

    Andre Kostur Guest

    (Tony Di Croce) wrote in
    news::

    > 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).
     
    Andre Kostur, Sep 29, 2004
    #3
  4. "Tony Di Croce" <> skrev i en meddelelse
    news:...
    > 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.

    >
    > Tony


    /Peter
     
    Peter Koch Larsen, Sep 29, 2004
    #4
  5. Tony Di Croce

    Andre Kostur Guest

    Andre Kostur <> wrote in
    news:Xns95735C19ECCECnntpspamkosturnet@207.35.177.134:

    > (Tony Di Croce) wrote in
    > news::
    >
    >> 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....
     
    Andre Kostur, Sep 29, 2004
    #5
  6. Tony Di Croce

    Maitre Bart Guest

    "Andre Kostur" <> wrote in message
    news:Xns95735DBB8BDF9nntpspamkosturnet@207.35.177.134...
    > Andre Kostur <> wrote in
    > news:Xns95735C19ECCECnntpspamkosturnet@207.35.177.134:
    >
    > > (Tony Di Croce) wrote in
    > > news::
    > >
    > >> 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.
     
    Maitre Bart, Sep 29, 2004
    #6
  7. Tony Di Croce

    Andre Kostur Guest

    "Maitre Bart" <> wrote in news:cjesqn$9ac$:

    >> >> 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....

    > 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...

    > 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.
     
    Andre Kostur, Sep 29, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,692
    Mike Schilling
    Mar 29, 2006
  2. Roger Leigh
    Replies:
    8
    Views:
    446
    Karl Heinz Buchegger
    Nov 17, 2003
  3. Replies:
    3
    Views:
    458
    Victor Bazarov
    Nov 10, 2004
  4. Vincent RICHOMME

    NULL pointer and null references

    Vincent RICHOMME, Jan 15, 2006, in forum: C++
    Replies:
    3
    Views:
    321
  5. jason
    Replies:
    13
    Views:
    883
    John B. Matthews
    May 14, 2010
Loading...

Share This Page