iterators::reference - what requirements on it are exposed?

Discussion in 'C++' started by Alexander Stippler, Dec 28, 2003.

  1. Hi,

    I've got a question concerning iterators. I'm writing some container class
    and iterators upon it. I have to have
    typedef typex pointer;
    typedef typey reference;
    to be standard conforming. But what semantic requirements does the standard
    expose on type typey (and typex). If I define
    reference operator*(),
    does the object I return have to be (non-const) referencable or can I return
    a temporary object of typey? If I'm not allowed to, how can I write some
    iterator and especially operator*(), if I have to create its result on the
    fly? I hope, I made my question understandable.

    regards,
    alex
     
    Alexander Stippler, Dec 28, 2003
    #1
    1. Advertising

  2. "Alexander Stippler" <-ulm.de> wrote in message
    news:-ulm.de...
    > Hi,
    >
    > I've got a question concerning iterators. I'm writing some container class
    > and iterators upon it. I have to have
    > typedef typex pointer;
    > typedef typey reference;
    > to be standard conforming. But what semantic requirements does the

    standard
    > expose on type typey (and typex). If I define
    > reference operator*(),


    I forget all the details as to why but apparently to be a standard container
    it turns out that pointer and reference have to be the obvious types (T*,
    T&) (perhaps const or volatile but not proxy classes).

    However you don't have to create standard containers and even the standard
    contains "non-standard" containers in particular vector<bool> precisely
    because reference isn't bool& and therefore you cannot take the address of
    one to get bool*.

    I think I MAY have come across all this in "Effective C++" or "More
    Effective C++"

    The main reason to give the typedef is so that standard algorithms can work
    with your iterators.
    If an algorith is supplied an iterator it cannot know directly what the
    fundamental type is so it generally needs to use the typedefs

    > does the object I return have to be (non-const) referencable or can I

    return
    > a temporary object of typey? If I'm not allowed to, how can I write some


    vector<bool> uses something that is not bool& and I don't know of any
    practical drawback.

    > iterator and especially operator*(), if I have to create its result on the
    > fly? I hope, I made my question understandable.
    >
    > regards,
    > alex
    >
    >
     
    Nick Hounsome, Dec 28, 2003
    #2
    1. Advertising

  3. Nick Hounsome wrote:

    >
    > "Alexander Stippler" <-ulm.de> wrote in message
    > news:-ulm.de...
    >> Hi,
    >>
    >> I've got a question concerning iterators. I'm writing some container
    >> class and iterators upon it. I have to have
    >> typedef typex pointer;
    >> typedef typey reference;
    >> to be standard conforming. But what semantic requirements does the

    > standard
    >> expose on type typey (and typex). If I define
    >> reference operator*(),

    >
    > I forget all the details as to why but apparently to be a standard
    > container it turns out that pointer and reference have to be the obvious
    > types (T*, T&) (perhaps const or volatile but not proxy classes).
    >
    > However you don't have to create standard containers and even the standard
    > contains "non-standard" containers in particular vector<bool> precisely
    > because reference isn't bool& and therefore you cannot take the address of
    > one to get bool*.
    >
    > I think I MAY have come across all this in "Effective C++" or "More
    > Effective C++"
    >
    > The main reason to give the typedef is so that standard algorithms can
    > work with your iterators.
    > If an algorith is supplied an iterator it cannot know directly what the
    > fundamental type is so it generally needs to use the typedefs
    >
    >> does the object I return have to be (non-const) referencable or can I

    > return
    >> a temporary object of typey? If I'm not allowed to, how can I write some

    >
    > vector<bool> uses something that is not bool& and I don't know of any
    > practical drawback.
    >
    >> iterator and especially operator*(), if I have to create its result on
    >> the fly? I hope, I made my question understandable.
    >>
    >> regards,
    >> alex
    >>
    >>


    Indeed both the icc-STL and gcc-STL do not return references for operator*()
    in some more general places, as I shortly noticed. Look at this, taken from
    reverse_iterator implementation:

    reference operator*() const
    { // return designated value
    _RanIt _Tmp = current;
    return (*--_Tmp);
    }

    Thanks for your answer. I want to use STL algorithms and that is exactly the
    extent to which I want to be standard conforming.

    regards,
    alex
     
    Alexander Stippler, Dec 29, 2003
    #3
    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. Learner
    Replies:
    5
    Views:
    657
    Karl Seguin
    Dec 21, 2005
  2. Maluk

    Private method exposed?

    Maluk, Apr 2, 2004, in forum: C++
    Replies:
    1
    Views:
    302
    Leor Zolman
    Apr 2, 2004
  3. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    519
    Kai-Uwe Bux
    May 8, 2005
  4. Martin Douglas

    Website Reference Aliases Not Exposed?

    Martin Douglas, Jan 4, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    324
    Martin Douglas
    Feb 13, 2007
  5. , India
    Replies:
    10
    Views:
    1,104
    James Kanze
    Aug 8, 2009
Loading...

Share This Page