473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to use functors as ordinary functions argument?

dear, all:

below is a function with a parameter of function pointer.

void f(int a, int b, int (*fp)(int, int))
{
std::cout << fp(a, b) << std::endl;
}
and f is called with functor plus as 3rd argument.

int x = 2, y = 3;
f(x, y, std::plus<int>());

but it raises an error, and how to solve it?

Thanks a lot.
Apr 1 '08 #1
9 3222
laikon wrote:
dear, all:

below is a function with a parameter of function pointer.

void f(int a, int b, int (*fp)(int, int))
{
std::cout << fp(a, b) << std::endl;
}
and f is called with functor plus as 3rd argument.

int x = 2, y = 3;
f(x, y, std::plus<int>());

but it raises an error, and how to solve it?
overload f.

#include <functional>
void f(int a, int b, const std::binary_function<int, int, int>& f)
{
std::cout << f(a,b) << std::endl;
}
Apr 1 '08 #2
red floyd wrote:
laikon wrote:
>dear, all:

below is a function with a parameter of function pointer.

void f(int a, int b, int (*fp)(int, int))
{
std::cout << fp(a, b) << std::endl;
}
and f is called with functor plus as 3rd argument.

int x = 2, y = 3;
f(x, y, std::plus<int>());

but it raises an error, and how to solve it?

overload f.

#include <functional>
void f(int a, int b, const std::binary_function<int, int, int>& f)
{
std::cout << f(a,b) << std::endl;
}
Or, since the function body is the same, use templates?

#include <iostream>

template <typename Tvoid f(int a, int b, const T &fp)
{
std::cout << fp(a, b) << std::endl;
}

int mult(int a, int b)
{
return a * b;
}

int main()
{
int x = 2, y = 3;

f(x, y, std::plus<int>());
f(x, y, mult);
}

Apr 1 '08 #3
laikon wrote:
dear, all:

below is a function with a parameter of function pointer.

void f(int a, int b, int (*fp)(int, int))
{
std::cout << fp(a, b) << std::endl;
}
and f is called with functor plus as 3rd argument.

int x = 2, y = 3;
f(x, y, std::plus<int>());

but it raises an error, and how to solve it?
Turn f into a template ?
Apr 1 '08 #4
laikon wrote:
dear, all:

below is a function with a parameter of function pointer.

void f(int a, int b, int (*fp)(int, int))
{
std::cout << fp(a, b) << std::endl;
}
and f is called with functor plus as 3rd argument.

int x = 2, y = 3;
f(x, y, std::plus<int>());

but it raises an error, and how to solve it?
Make it a template:

template<typename Functor>
void f(int a, int b, Functor fp)
{
std::cout << fp(a, b) << std::endl;
}
Apr 1 '08 #5
On 2008-04-01 19:00, laikon wrote:
dear, all:

below is a function with a parameter of function pointer.

void f(int a, int b, int (*fp)(int, int))
{
std::cout << fp(a, b) << std::endl;
}
and f is called with functor plus as 3rd argument.

int x = 2, y = 3;
f(x, y, std::plus<int>());

but it raises an error, and how to solve it?
Use a function, things that uses functors are built using templates.

--
Erik Wikström
Apr 1 '08 #6
On 2008-04-01 13:50:03 -0400, red floyd <no*****@here.dudesaid:
>
overload f.

#include <functional>
void f(int a, int b, const std::binary_function<int, int, int>& f)
{
std::cout << f(a,b) << std::endl;
}
That won't work. std::binary_function has three typedefs, nothing more.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Apr 1 '08 #7
Pete Becker wrote:
On 2008-04-01 13:50:03 -0400, red floyd <no*****@here.dudesaid:
>>
overload f.

#include <functional>
void f(int a, int b, const std::binary_function<int, int, int>& f)
{
std::cout << f(a,b) << std::endl;
}

That won't work. std::binary_function has three typedefs, nothing more.
Oops... you're right. My bad.
Apr 1 '08 #8
Juha Nieminen wrote:
laikon wrote:
>dear, all:

below is a function with a parameter of function pointer.

void f(int a, int b, int (*fp)(int, int))
{
std::cout << fp(a, b) << std::endl;
}
and f is called with functor plus as 3rd argument.

int x = 2, y = 3;
f(x, y, std::plus<int>());

but it raises an error, and how to solve it?

Make it a template:

template<typename Functor>
void f(int a, int b, Functor fp)
{
std::cout << fp(a, b) << std::endl;
}
Actually, since the OP wanted an int return:

template<typename Functor>
void f(int a, int b, Functor fp)
{
std::cout << static_cast<int>(fp(a,b)) << std::endl;
}
Apr 2 '08 #9
Kai-Uwe Bux wrote:
I guess, one could devise some template magic to implement and check the
conceptual requirement that Functor has an operator( int, int ) that
returns an int. However, I think that would be overkill in most cases.
If I'm not mistaken, this will become easy with the next C++ standard.
Apr 2 '08 #10

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

Similar topics

41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
2
by: nsgi_2004 | last post by:
Hi, I have been learning about functors and at first everything was clear. Make a class and overload operator () so that an object of the functor can be thought of as a function. However, I...
2
by: baumann | last post by:
hi all, i am reading the C++ templates complete guide, i have question on the following statement. We must therefore make sure the template parameters of the class template appear in the type...
4
by: Fraser Ross | last post by:
Functors taking 1 argument for operator() should inherit from unary_function and those with 2 arguments should inherit from binary_function. If a functor has zero arguments for its operator()...
2
by: leaf | last post by:
i have a struct which defines a function call stuct TFunction { int id; // ID of the function to call UINT nparams; // No. of parameters ULONG* params // Parameter...
2
by: Martin Herbert Dietze | last post by:
Hi, in a project I am using callbacks which are called like ordinary functions but in fact are Loki::Functor's encapsulating calls to non-static member functions on instances of different...
4
by: tryptik | last post by:
Hello all, I have a question about iterators. I have a container of functors that operate on an std::string. The functors go something like this: class Functor { std::string...
2
by: Jon Slaughter | last post by:
I'm trying to mess with functors and the way I want it to work is that when I create a functor it will automatically add itself to an array. The attached code demonstrates what I mean. The...
4
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.