postfix operator++(int) with const

Discussion in 'C++' started by Chris Forone, Jun 20, 2008.

  1. Chris Forone

    Chris Forone Guest

    hello group,

    why i cant make the InputIterator of the following func-temp const?

    template<typename InputIterator, typename OutputIterator>
    OutputIterator Types::Copy(InputIterator source,
    OutputIterator first, OutputIterator last)
    {
    while (first != last)
    *first++ = *source++;

    return last;
    }

    thanks & hand, chris
     
    Chris Forone, Jun 20, 2008
    #1
    1. Advertising

  2. Chris Forone

    Road.Tang Guest

    On Jun 20, 4:01 pm, Chris Forone <> wrote:
    > hello group,
    >
    > why i cant make the InputIterator of the following func-temp const?
    >
    > template<typename InputIterator, typename OutputIterator>
    > OutputIterator Types::Copy(InputIterator source,
    > OutputIterator first, OutputIterator last)
    > {
    > while (first != last)
    > *first++ = *source++;
    >
    > return last;
    >
    > }
    >
    > thanks & hand, chris


    Not very understand what you mean.

    and, note one of the standard copy declaration is
    template<typename In, typename Out>
    Out copy(In first, In last, Out res)

    -road
     
    Road.Tang, Jun 20, 2008
    #2
    1. Advertising

  3. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Chris Forone wrote:
    > hello group,
    >
    > why i cant make the InputIterator of the following func-temp const?
    >
    > [...]
    > *first++ = *source++;

    ^^
    > [...]


    You are post-incrementing the "source" variable, so it obviously cannot
    be constant. If you need it to be const for whatever reason (eg. someone
    else wrote the header and you cannot change it), just make a copy of it
    (assuming the InputIterator::eek:perator= is defined and works as expected):

    InputIterator sourceCopy = source;
    while (first != last)
    *first++ = *sourceCopy++;

    Otherwise, passing by value as in your current implementation is just fine.

    I suppose, though, that you meant "const" as "iterator cannot be used to
    change pointed value". I'm afraid you have to define a
    ConstInputIterator for that.

    Cheers,
    - -Federico
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (GNU/Linux)
    Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

    iD4DBQFIW2fNBIpu+y7DlLcRAnqyAJ4iyDYO1eUQMTsrR8aIQDKTiqY4KwCYlC8c
    E+Y9XR4j/kCu3kk320KOfg==
    =z4Ib
    -----END PGP SIGNATURE-----
     
    Federico Zenith, Jun 20, 2008
    #3
  4. Chris Forone

    dizzy Guest

    Chris Forone wrote:

    > hello group,
    >
    > why i cant make the InputIterator of the following func-temp const?
    >
    > template<typename InputIterator, typename OutputIterator>
    > OutputIterator Types::Copy(InputIterator source,
    > OutputIterator first, OutputIterator last)
    > {
    > while (first != last)
    > *first++ = *source++;
    >
    > return last;
    > }


    That code should work fine. I think you are confusing "const iterators" as
    concept with "const iterators" as const values of an iterator object. They
    are not the same thing.

    The former means an iterator to const objects (that points and iterates over
    const objects). The iterator itself cannot be const in order to iterator
    (ie to modify it calling operator++(int) over it). Thus, when Copy() is
    called with a const iterator value the "InputIterator" type is deduced to
    be such a const iterator value and then obviously you can't call
    operator++(int) over it (since that modifies objects but since iterator is
    const it can't).

    I'm talking about the differences between:

    container::iterator it; // mutable iterator to mutable values
    container::iterator const c_it; // const iterator to mutable values
    container::const_iterator cit; // mutable iterator to const values
    container::const_iterator const c_cit; // const iterator to const values

    For the "mutable iterators" you can modify them, thus you can call
    operator++(int) on them fine. For the iterators to mutable values you can
    modify the pointed to values.

    What you want to call Copy() with as an InputIterator is a mutable iterator
    to a const value (so the iterator can be modified with operator++(int) but
    the pointed to values can't).

    --
    Dizzy
     
    dizzy, Jun 20, 2008
    #4
  5. Chris Forone

    Chris Forone Guest

    Federico Zenith schrieb:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Chris Forone wrote:
    >> hello group,
    >>
    >> why i cant make the InputIterator of the following func-temp const?
    >>
    >> [...]
    >> *first++ = *source++;

    > ^^
    >> [...]

    >
    > You are post-incrementing the "source" variable, so it obviously cannot
    > be constant. If you need it to be const for whatever reason (eg. someone
    > else wrote the header and you cannot change it), just make a copy of it
    > (assuming the InputIterator::eek:perator= is defined and works as expected):
    >
    > InputIterator sourceCopy = source;
    > while (first != last)
    > *first++ = *sourceCopy++;
    >
    > Otherwise, passing by value as in your current implementation is just fine.
    >
    > I suppose, though, that you meant "const" as "iterator cannot be used to
    > change pointed value". I'm afraid you have to define a
    > ConstInputIterator for that.
    >
    > Cheers,
    > - -Federico
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.4.5 (GNU/Linux)
    > Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org
    >
    > iD4DBQFIW2fNBIpu+y7DlLcRAnqyAJ4iyDYO1eUQMTsrR8aIQDKTiqY4KwCYlC8c
    > E+Y9XR4j/kCu3kk320KOfg==
    > =z4Ib
    > -----END PGP SIGNATURE-----


    thanks a lot, thats it!

    cheers, chris
     
    Chris Forone, Jun 20, 2008
    #5
  6. Chris Forone

    Chris Forone Guest

    dizzy schrieb:
    > Chris Forone wrote:
    >
    >> hello group,
    >>
    >> why i cant make the InputIterator of the following func-temp const?
    >>
    >> template<typename InputIterator, typename OutputIterator>
    >> OutputIterator Types::Copy(InputIterator source,
    >> OutputIterator first, OutputIterator last)
    >> {
    >> while (first != last)
    >> *first++ = *source++;
    >>
    >> return last;
    >> }

    >
    > That code should work fine. I think you are confusing "const iterators" as
    > concept with "const iterators" as const values of an iterator object. They
    > are not the same thing.
    >
    > The former means an iterator to const objects (that points and iterates over
    > const objects). The iterator itself cannot be const in order to iterator
    > (ie to modify it calling operator++(int) over it). Thus, when Copy() is
    > called with a const iterator value the "InputIterator" type is deduced to
    > be such a const iterator value and then obviously you can't call
    > operator++(int) over it (since that modifies objects but since iterator is
    > const it can't).
    >
    > I'm talking about the differences between:
    >
    > container::iterator it; // mutable iterator to mutable values
    > container::iterator const c_it; // const iterator to mutable values
    > container::const_iterator cit; // mutable iterator to const values
    > container::const_iterator const c_cit; // const iterator to const values
    >
    > For the "mutable iterators" you can modify them, thus you can call
    > operator++(int) on them fine. For the iterators to mutable values you can
    > modify the pointed to values.
    >
    > What you want to call Copy() with as an InputIterator is a mutable iterator
    > to a const value (so the iterator can be modified with operator++(int) but
    > the pointed to values can't).
    >


    thanks a lot, thats it!

    cheers, chris
     
    Chris Forone, Jun 20, 2008
    #6
  7. Chris Forone

    Chris Forone Guest

    Road.Tang schrieb:
    > On Jun 20, 4:01 pm, Chris Forone <> wrote:
    >> hello group,
    >>
    >> why i cant make the InputIterator of the following func-temp const?
    >>
    >> template<typename InputIterator, typename OutputIterator>
    >> OutputIterator Types::Copy(InputIterator source,
    >> OutputIterator first, OutputIterator last)
    >> {
    >> while (first != last)
    >> *first++ = *source++;
    >>
    >> return last;
    >>
    >> }
    >>
    >> thanks & hand, chris

    >
    > Not very understand what you mean.
    >
    > and, note one of the standard copy declaration is
    > template<typename In, typename Out>
    > Out copy(In first, In last, Out res)
    >
    > -road


    i know the std::copy in std::algorithm. i need a target-limited version
    to copy with streambuf_iterator to std::valarray in multiple steps...

    cheers, chris
     
    Chris Forone, Jun 20, 2008
    #7
  8. Chris Forone

    James Kanze Guest

    On Jun 20, 10:01 am, Chris Forone <> wrote:

    > why i cant make the InputIterator of the following func-temp
    > const?


    > template<typename InputIterator, typename OutputIterator>
    > OutputIterator Types::Copy(InputIterator source,
    > OutputIterator first, OutputIterator last)
    > {
    > while (first != last)
    > *first++ = *source++;
    > return last;
    > }


    Because you modify it in the function. You can instantiate the
    algorithm with const_iterator, however, if that's what you want.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 20, 2008
    #8
    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. Timo Freiberger
    Replies:
    3
    Views:
    987
    Bob Hairgrove
    Oct 30, 2004
  2. Tim Clacy
    Replies:
    15
    Views:
    2,736
    Kanenas
    May 30, 2005
  3. Replies:
    11
    Views:
    1,143
  4. Javier
    Replies:
    2
    Views:
    603
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,158
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page