473,484 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

postfix operator++(int) with const

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
Jun 27 '08 #1
5 2072
On Jun 20, 4:01 pm, Chris Forone <4...@gmx.atwrote:
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
Jun 27 '08 #2
-----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::operator= 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+y7DlLcRAnqyAJ4iyDYO1eUQMTsrR8aIQD KTiqY4KwCYlC8c
E+Y9XR4j/kCu3kk320KOfg==
=z4Ib
-----END PGP SIGNATURE-----
Jun 27 '08 #3
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::operator= 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+y7DlLcRAnqyAJ4iyDYO1eUQMTsrR8aIQD KTiqY4KwCYlC8c
E+Y9XR4j/kCu3kk320KOfg==
=z4Ib
-----END PGP SIGNATURE-----
thanks a lot, thats it!

cheers, chris
Jun 27 '08 #4
Road.Tang schrieb:
On Jun 20, 4:01 pm, Chris Forone <4...@gmx.atwrote:
>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
Jun 27 '08 #5
On Jun 20, 10:01 am, Chris Forone <4...@gmx.atwrote:
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:ja*********@gmail.com
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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2348
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
15
4351
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
2
2871
by: Claudius | last post by:
Hello, I have written a class A with the access operator(int) overloaded by a A-const version which returns an int by value: ------------------------------------------------ #include...
0
1738
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
4
4836
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename...
1
2093
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
1
6813
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4845
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3046
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3041
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1359
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
592
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
235
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.