473,609 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ 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<typena me InputIterator, typename OutputIterator>
OutputIterator Types::Copy(Inp utIterator source,
OutputIterator first, OutputIterator last)
{
while (first != last)
*first++ = *source++;

return last;
}

thanks & hand, chris
Jun 27 '08 #1
5 2091
On Jun 20, 4:01 pm, Chris Forone <4...@gmx.atwro te:
hello group,

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

template<typena me InputIterator, typename OutputIterator>
OutputIterator Types::Copy(Inp utIterator 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<typena me 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
ConstInputItera tor for that.

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

iD4DBQFIW2fNBIp u+y7DlLcRAnqyAJ 4iyDYO1eUQMTsrR 8aIQDKTiqY4KwCY lC8c
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
ConstInputItera tor for that.

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

iD4DBQFIW2fNBIp u+y7DlLcRAnqyAJ 4iyDYO1eUQMTsrR 8aIQDKTiqY4KwCY lC8c
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.atwro te:
>hello group,

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

template<typen ame InputIterator, typename OutputIterator>
OutputIterat or Types::Copy(Inp utIterator 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<typena me 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_itera tor to std::valarray in multiple steps...

cheers, chris
Jun 27 '08 #5
On Jun 20, 10:01 am, Chris Forone <4...@gmx.atwro te:
why i cant make the InputIterator of the following func-temp
const?
template<typena me InputIterator, typename OutputIterator>
OutputIterator Types::Copy(Inp utIterator 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 objektorientier ter Datenverarbeitu ng
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
2369
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
4367
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
2884
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 <iostream> using namespace std;
0
1761
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 implementations of a member function of A that takes as an argument the abstract class Base (see Option 1 / Option 2): - Both compile without problems - Only the second works in python (see python program below).
4
4851
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 _Predicate::argument_type argument_type; typedef typename _Predicate::return_type return_type; _Predicate pred_;
1
2110
by: developereo | last post by:
Hi folks, Can somebodyshed some light on this problem? class Interface { protected: Interface() { ...} virtual ~Interface() { ... } public:
0
8035
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8534
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8188
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8374
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6034
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5502
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4059
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.