#include <iostream>
#include <algorithm>
struct functor {
~functor() {std::cout << 'D';}
void operator()(int a) {std::cout << a;}
};
int main()
{
int a[] = {1,2,3,4,5};
functor f;
std::for_each(a,a+5,f);
}
// produces : 12345DDD
notice the three D's in the output.
The functor is copied once as pass-by-value to for_each()
and once again as the return value from for_each().
Is there a way to use for_each without introducing these
extra copies of the functor? Perhaps this is just a
g++ implementation issue?
Regards,
Sean 2 5078
"Sean" <se******@rogers.com> wrote... #include <iostream> #include <algorithm> struct functor { ~functor() {std::cout << 'D';} void operator()(int a) {std::cout << a;} }; int main() { int a[] = {1,2,3,4,5}; functor f; std::for_each(a,a+5,f); } // produces : 12345DDD
notice the three D's in the output.
The functor is copied once as pass-by-value to for_each() and once again as the return value from for_each().
Is there a way to use for_each without introducing these extra copies of the functor? Perhaps this is just a g++ implementation issue?
Try
int a[] = {1,2,3,4,5};
const functor &f = functor();
std::for_each(a,a+5,f);
Victor
"Howard Hinnant" <hi*****@metrowerks.com> wrote in message
news:300620031912177840%hi*****@metrowerks.com...
<snip> You could use explicit template arguments to specify pass-by-reference for for_each:
#include <iostream> #include <algorithm>
struct functor { ~functor() {std::cout << 'D';} void operator()(int a) const {std::cout << a;} };
int main() { int a[] = {1,2,3,4,5}; functor f; std::for_each<int*, functor&>(a,a+5,f); }
12345D
Not all algorithms may behave as you want, even with this extra effort. For example see:
http://anubis.dkuug.dk/jtc1/sc22/wg2...active.html#92
However, if you happen to be a Metrowerks customer, and you're getting unwanted functor copying even when passing by reference via explicit template arguments, feel free to report it as a bug to me. We endeavor to make this idiom work.
-- Howard Hinnant Metrowerks
Thanks for this helpful information Howard. It seems to me that
pass-by-reference should be the default behavior for for_each. But after
considering the information at the link you provided i can understand
why it is not.
Cheers,
Sean This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sks_cpp |
last post by:
The for_each library function - does it throw any exceptions if the functor
you pass it DOESN'T throw anything.
Example:
class Functor
{
bool operator()(int a) { if (a>10) return true; }
};
...
|
by: Alex Vinokur |
last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value.
Might it make sense to have also algorithms for_each2() and transform2()
which pass Functor-parameters by...
|
by: John Black |
last post by:
Hi,
I am not familiar with for_each very well, suppoase I have a
vector<pair<unsigned int, unsigned int> > vec1 and the contents are
{<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>}
what...
|
by: franklini |
last post by:
hello people,
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.
i have written this abtract class shape and its...
|
by: Michal Wyrebski |
last post by:
Hello,
I'm new in this group and I don't know if my questions are too silly,
but I'm intermediate programmer and don't have enought experience.
Please be charitable.
I don't know how to write...
|
by: shaun |
last post by:
I am working on code where I am handed a vector of pointers vector<T*>
or a map<std::string, T*>, and I have to delete the objects and set the
pointers to zero. I have been using a 'for' loop and...
|
by: Fraser Ross |
last post by:
Is it wrong to describe for_each as a modifying algorithm? It is
described as one here: http://www.josuttis.com/libbook/algolist.pdf.
transform appears to be the algorithm to use for modifying...
|
by: Chris Roth |
last post by:
I have a vector of vectors:
vector< vector<double v;
and have initialized it with:
v( 5 );
as I know I will have 5 columns of data. At this point, I read text file
data into each of the the...
|
by: nguillot |
last post by:
Hello
I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):
typedef std::map<k, dtMap;
struct setIntMember
{
setIntMember(int j) :...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |