I've got following code test C++'s functor. For the sake of
easy-reading, I omit some declearations.
#include <algorithm>
#include <functional>
using namespace std;
template <typename Tclass Sum{
public:
Sum(T i=0):sum(i){};
inline void operator () (T x){
sum += x;
}
inline T output() const{
return sum;
}
private:
T sum;
};
int main( void ){
vector<intvec(10, 1);
Sum<intsum;
sum = for_each(vec.begin(), vec.end(), Sum<int>()); .......(1)
sum = for_each(vec.begin(), vec.end(), sum); .........(2)
sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)
cout << sum.output() << endl;
return 1;
}
It's easy to understand that (2) works, because sum.operator()(int) is
called implicitly. (1) also works, it confuses me. Does Sum<int>()
create an implicit class Sum object? On the other hand, Sum<int>
doesn't work.
(3) fails, does it means the third argument of "for_each" couldn't be a
function pointer? What will the code be if I want to pass a function
pointer here? Thanks for help! 2 1663
On 22 Jan 2007 22:50:56 -0800, hn.ft.pris wrote:
>I've got following code test C++'s functor. For the sake of easy-reading, I omit some declearations.
#include <algorithm> #include <functional>
using namespace std;
template <typename Tclass Sum{ public: Sum(T i=0):sum(i){}; inline void operator () (T x){ sum += x; } inline T output() const{ return sum; }
private: T sum; };
This is a (probably) problematic 'stateful predicate', see: http://www.gotw.ca/gotw/052.htm.
>int main( void ){
vector<intvec(10, 1);
Sum<intsum;
sum = for_each(vec.begin(), vec.end(), Sum<int>()); .......(1) sum = for_each(vec.begin(), vec.end(), sum); .........(2) sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)
cout << sum.output() << endl;
return 1; }
It's easy to understand that (2) works, because sum.operator()(int) is called implicitly. (1) also works, it confuses me. Does Sum<int>() create an implicit class Sum object? On the other hand, Sum<int> doesn't work. (3) fails, does it means the third argument of "for_each" couldn't be a function pointer? What will the code be if I want to pass a function pointer here? Thanks for help!
The third argument in (3) is neither a function object nor a pointer
to a (free) function.
Best wishes,
Roland Pibinger
sum = for_each(vec.begin(), vec.end(), sum.operator()(int) ); ...(3)
(3) fails, does it means the third argument of "for_each" couldn't be a
function pointer? What will the code be if I want to pass a function
pointer here? Thanks for help!
sum.operator()(int) isn't valid. You can't take an address of a member
function from an instance, you have to take it from the class. As such
its signature changes from what it was, (int) in this case, to
accepting a pointer to an instance of that class...so (Sum*, int) in
this case. To use such in an algorithm you need a binder. I use TR1
bind, which can be found as boost::bind.
sum = for_each(vec.begin(), vec.end(), boost::bind(&Sum::operator(),
&sum, _1)); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: What-a-Tool |
last post by:
How does the expire date work setting it server side with asp.
I know with javascript setting it client side it will be set to the clients
local time, and therefore expire when the clients local...
|
by: ma740988 |
last post by:
I'm perusing a slide with roughly 12 bullets spread across 3 pages.
Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more
specifically the bullet that states:
" Avoid the STL unless...
|
by: Gregory Guthrie |
last post by:
I am comparing Python to a few other scripting languages, and used a simple
anagrams program as a sample.
I was surprised ast a few python features that did not work as I would
expect/wish;...
|
by: Chad |
last post by:
The problem is:
Write a recursive version of the function reverse(s), which reverses
the string s in place.
In "The C Answer Book", Second Edition, near the bottom of page 95, the
authors say...
|
by: petermichaux |
last post by:
Hi,
I have a general JavaScript library API design question based on some
examples I have seen. There seem to be two options
The first option is used by the AJAX libraries and by Matt Kruse's...
|
by: hn.ft.pris |
last post by:
I've got following code test C++'s functor. For the sake of
easy-reading, I omit some declearations.
#include <algorithm>
#include <functional>
using namespace std;
|
by: nikki_herring |
last post by:
I am using setTimeout( ) to continuously call a function that randomly
rotates/displays 2 images on a page. The part I need help with is the
second image should rotate 3 seconds after the first...
|
by: Tom Cole |
last post by:
I'm working on a small Ajax request library to simplify some tasks
that I will be taking on shortly. For the most part everything works
fine, however I seem to have some issues when running two...
|
by: Joe Mayo |
last post by:
I think I become more and more alone...
Everybody tells me that C++ is better, because once a project becomes
very large, I should be happy that it has been written in C++ and not C.
I'm the only...
|
by: Frank Stutzman |
last post by:
My apologies in advance, I'm new to python
Say, I have a dictionary that looks like this:
record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16',
'FF': '3.9', 'C3': '343', 'E4': '1157',...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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:
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...
| |