postfix operator++(int) with const

C

Chris Forone

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
 
R

Road.Tang

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
 
F

Federico Zenith

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

Chris said:
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-----
 
D

dizzy

Chris said:
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).
 
C

Chris Forone

Federico said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris said:
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
 
C

Chris Forone

dizzy said:
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
 
C

Chris Forone

Road.Tang said:
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
 
J

James Kanze

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top