why both value_type and reference

Discussion in 'C++' started by Andrew, Mar 7, 2006.

  1. Andrew

    Andrew Guest

    Hello:

    Can someone please elaborate on why the member types 'value_type',
    'reference', and 'const_reference' are all defined in a standard STL
    container. Why not just define
    'value_type' and then use 'value_type&' and 'const value_type&'.

    Also, if we define these other types, why not define
    'const_value_type', etc....

    Thanks,
    AKH
     
    Andrew, Mar 7, 2006
    #1
    1. Advertising

  2. Andrew wrote:
    > Can someone please elaborate on why the member types 'value_type',
    > 'reference', and 'const_reference' are all defined in a standard STL
    > container. Why not just define
    > 'value_type' and then use 'value_type&' and 'const value_type&'.


    If you'd like you're free to use 'value_type&'. Nobody's to stop you.
    The standard containers do not typedef 'reference_type' to 'value_type&'
    because in some cases 'reference_type' is a special *proxy* type, and
    not a simple C++ reference.

    > Also, if we define these other types, why not define
    > 'const_value_type', etc....


    Because that would be excessive.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 7, 2006
    #2
    1. Advertising

  3. Andrew

    Andrew Guest

    Victor Bazarov wrote:
    > If you'd like you're free to use 'value_type&'. Nobody's to stop you.


    Client's perspective:
    If the designer of the container is using a proxy, shouldn't I use
    'reference_type' instead?

    > The standard containers do not typedef 'reference_type' to 'value_type&'
    > because in some cases 'reference_type' is a special *proxy* type, and
    > not a simple C++ reference.


    Library designer's perspective:
    How does the designer of a container ensure that such a proxy type will
    be used if the client could inadvertently use 'value_type&' instead?

    I suppose to be on the safe side one should *always* use
    'reference_type'
    instead of 'value_type&', although that appears to be an easy oversight
    if
    one uses typedefs heavily. Could you please give me an example in the
    STL
    where reference_type is neccessary.

    Thanks again for the quick reply,
    AKH
     
    Andrew, Mar 7, 2006
    #3
  4. Andrew

    Pete Becker Guest

    Andrew wrote:

    > Hello:
    >
    > Can someone please elaborate on why the member types 'value_type',
    > 'reference', and 'const_reference' are all defined in a standard STL
    > container. Why not just define
    > 'value_type' and then use 'value_type&' and 'const value_type&'.
    >


    Originally, it was because under 16-bit windows, pointers and references
    could be qualified with "near" and "far", to reflect where the actual
    data resided. If you had a container that stored its data in a way that
    required far pointers and far references and the program was compiled
    with the default set to near, then value_type* and value_type& wouldn't
    work.

    More generally, allowing the pointer type and reference type to differ
    from the defaults allows more general memory management schemes.

    --

    Pete Becker
    Roundhouse Consulting, Ltd.
     
    Pete Becker, Mar 7, 2006
    #4
  5. Andrew wrote:
    > Victor Bazarov wrote:
    >
    >>If you'd like you're free to use 'value_type&'. Nobody's to stop you.

    >
    >
    > Client's perspective:
    > If the designer of the container is using a proxy, shouldn't I use
    > 'reference_type' instead?


    There is no answer to your question without any context. Both have the
    right to exist because both are used, in different contexts. If you need
    a reference to the type that is the stored value, use 'value_type &'. If
    you need a way to modify the stored value directly, most likely you need
    to use 'reference_type'.

    What book on the Standard Library are you reading that doesn't explain
    those things?

    >>The standard containers do not typedef 'reference_type' to 'value_type&'
    >>because in some cases 'reference_type' is a special *proxy* type, and
    >>not a simple C++ reference.

    >
    >
    > Library designer's perspective:
    > How does the designer of a container ensure that such a proxy type will
    > be used if the client could inadvertently use 'value_type&' instead?


    There is no protection against stupidity.

    > I suppose to be on the safe side one should *always* use
    > 'reference_type'
    > instead of 'value_type&', although that appears to be an easy oversight
    > if
    > one uses typedefs heavily. Could you please give me an example in the
    > STL
    > where reference_type is neccessary.


    vector<bool>.

    And get yourself a copy of "The C++ Standard Library" by Josuttis.

    V
    --
    Please remove capital As from my address when replying by mail
     
    Victor Bazarov, Mar 7, 2006
    #5
    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. Chyi Pin Lim

    value_type of a back_insert_iterator

    Chyi Pin Lim, Dec 5, 2003, in forum: C++
    Replies:
    9
    Views:
    530
    ppLiu_china
    Dec 11, 2003
  2. chris
    Replies:
    8
    Views:
    500
    Tom Widmer
    Dec 10, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,030
    Smokey Grindel
    Dec 2, 2006
  4. Weng Tianxiang
    Replies:
    6
    Views:
    795
    Ray Andraka
    Mar 19, 2007
  5. Hatzigiannakis Nikos

    value_type and pair classes

    Hatzigiannakis Nikos, Feb 21, 2008, in forum: C++
    Replies:
    1
    Views:
    332
    Kai-Uwe Bux
    Feb 21, 2008
Loading...

Share This Page