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. 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;
}
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);
}
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 ?
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;
}
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
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)
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.
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;
}
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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()...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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: 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:
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: 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...
| |