I have a function which takes a functor argument. I which to call it for a
functor which is actually a class member; this works fine, using
the mem_fun_ref and bind1st functions (see listing 1 below). Or, rather,
it works fine as long as my member functor is const. The problem comes
when I wish to use it for a *non*-const functor (see listing 2 below):
*** Start listing 1 ************************************************** *
// test1.cpp
#include <iostream>
// function taking (ref. to) functor
template<typename Fdouble bar(const F& f, double x, double y)
{
return f(x)+y;
}
// class with member functor
class Object
{
private:
double a, b;
public:
Object(double a_, double b_) : a(a_), b(b_) {}
// the (const) member functor
double foo(double x) const {return a*x+b;}
};
int main(int argc, char* argv[])
{
using namespace std;
// create object
Object object(2.0,3.0);
// create functor ref from member functor "foo", bind to object
// and pass to function "bar"
double z = bar(bind1st(mem_fun_ref(&Object::foo),object),1.0, 2.0);
cout << "z = " << z << '\n';
return 0;
}
*** End listing 1 ************************************************** ***
$ g++ test1.cpp -o test1
$ ./test1
z = 7
So far so good. Now I try to make the member functor "foo" non-const:
*** Start listing 2 ************************************************** *
// test2.cpp
#include <iostream>
// function taking (ref. to) functor
template<typename Fdouble bar(F& f, double x, double y)
{
return f(x)+y;
}
// class with member functor
class Object
{
private:
double a, b;
public:
Object(double a_, double b_) : a(a_), b(b_) {}
// the (non-const) member functor
double foo(double x) {return a*x+b;}
};
int main(int argc, char* argv[])
{
using namespace std;
// create object
Object object(2.0,3.0);
// create functor ref from member functor "foo", bind to object
// and pass to function "bar"
double z = bar(bind1st(mem_fun_ref(&Object::foo),object),1.0, 2.0);
cout << "z = " << z << '\n';
return 0;
}
*** End listing 2 ************************************************** ***
Note that I have made the first argument of bar now a *non* const ref:
$ g++ test2.cpp -o test2
test2.cpp: In function ‘int main(int, char**)’:
test2.cpp:33: error: no matching function for call to ‘bar(std::binder1st<std::mem_fun1_ref_t<double, Object, double, double, double)’
test2.cpp:6: note: candidates are: double bar(F&, double, double) [with F = std::binder1st<std::mem_fun1_ref_t<double, Object, double]
I don't understand this, as I would have thought that there'd be sensible
non-const overloads for the binder and mem_fun routines...
Any help much appreciated.
Using gcc 4.1.1 on RHEL linux on x86_64
--
Lionel B 2 2482
Lionel B wrote:
I have a function which takes a functor argument. I which to call it for a
functor which is actually a class member; this works fine, using
the mem_fun_ref and bind1st functions (see listing 1 below). Or, rather,
it works fine as long as my member functor is const. The problem comes
when I wish to use it for a *non*-const functor (see listing 2 below):
*** Start listing 1 ************************************************** *
// test1.cpp
#include <iostream>
// function taking (ref. to) functor
template<typename Fdouble bar(const F& f, double x, double y)
{
return f(x)+y;
}
// class with member functor
class Object
{
private:
double a, b;
public:
Object(double a_, double b_) : a(a_), b(b_) {}
// the (const) member functor
double foo(double x) const {return a*x+b;}
};
int main(int argc, char* argv[])
{
using namespace std;
// create object
Object object(2.0,3.0);
// create functor ref from member functor "foo", bind to object
// and pass to function "bar"
double z = bar(bind1st(mem_fun_ref(&Object::foo),object),1.0, 2.0);
cout << "z = " << z << '\n';
return 0;
}
*** End listing 1 ************************************************** ***
$ g++ test1.cpp -o test1
$ ./test1
z = 7
So far so good. Now I try to make the member functor "foo" non-const:
*** Start listing 2 ************************************************** *
// test2.cpp
#include <iostream>
// function taking (ref. to) functor
template<typename Fdouble bar(F& f, double x, double y)
{
return f(x)+y;
}
// class with member functor
class Object
{
private:
double a, b;
public:
Object(double a_, double b_) : a(a_), b(b_) {}
// the (non-const) member functor
double foo(double x) {return a*x+b;}
};
int main(int argc, char* argv[])
{
using namespace std;
// create object
Object object(2.0,3.0);
// create functor ref from member functor "foo", bind to object
// and pass to function "bar"
double z = bar(bind1st(mem_fun_ref(&Object::foo),object),1.0, 2.0);
cout << "z = " << z << '\n';
return 0;
}
*** End listing 2 ************************************************** ***
Note that I have made the first argument of bar now a *non* const ref:
Switch both bind1st and mem_fun_ref over to boost and it works with
non-const member function.
However, it remains to be seen if you get what you seek. bar's first
arguement must be const.
#include <boost/functional.hpp>
replace
using namepsace std;
with
using boost::binder1st;
using boost::mem_fun_ref;
>
$ g++ test2.cpp -o test2
test2.cpp: In function 'int main(int, char**)':
test2.cpp:33: error: no matching function for call to 'bar(std::binder1st<std::mem_fun1_ref_t<double, Object, double, double, double)'
test2.cpp:6: note: candidates are: double bar(F&, double, double) [with F = std::binder1st<std::mem_fun1_ref_t<double, Object, double]
I don't understand this, as I would have thought that there'd be sensible
non-const overloads for the binder and mem_fun routines...
Any help much appreciated.
Using gcc 4.1.1 on RHEL linux on x86_64
--
Lionel B
On Fri, 01 Dec 2006 08:11:56 -0800, Salt_Peter wrote:
Lionel B wrote:
>I have a function which takes a functor argument. I which to call it for a functor which is actually a class member; this works fine, using the mem_fun_ref and bind1st functions (see listing 1 below). Or, rather, it works fine as long as my member functor is const. The problem comes when I wish to use it for a *non*-const functor (see listing 2 below):
[snip]
>
Switch both bind1st and mem_fun_ref over to boost and it works with
non-const member function.
However, it remains to be seen if you get what you seek. bar's first
arguement must be const.
#include <boost/functional.hpp>
replace
using namepsace std;
with
using boost::binder1st;
using boost::mem_fun_ref;
Yes, it works alright - thanks.
Is this just down to a dodgy implementation of binder1st and mem_fun_ref? I
tested my original code using Intel's compiler (icc 9.0 - which
generally seems to be pretty good on standards support) and it failed in
a similar way to gcc 4.1.
--
Lionel B This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Roy Yao |
last post by:
Hello,
I need to pass a pointer to a
callback function to the lower
level modules. But the function
is thought to be a virtual member one.
How can I get the real address of
the virtual...
|
by: slide_o_mix |
last post by:
I have a class that has an array of function pointers as a member
variable. I pass in function pointers in the constructor (these function
pointers are to C functions). Later when I try and...
|
by: Dave |
last post by:
Hello all,
Are the rules for templated member functions different in any way than the
rules for templated non-member functions?
How about templated member classes Vs. templated non-member...
|
by: Gregory Bond |
last post by:
I'm building a class hierarchy that needs to keep as a class variable a
reference to a (non-member) function, so that different subclasses can
use different generator functions. But it seems...
|
by: Enquiries, Hopkins Research |
last post by:
Hi all
I have a conundrum that is puzzling me.
I have a large codebase in C that I am converting to C++ as fast as possible
(i.e. slowly because I keep learning new idioms and stumbling with...
|
by: ghager |
last post by:
Hi all,
I must be blind or stupid. Please consider the following code:
----
....
template <class T> class P;
template <class T> P<T> operator*(T,const P<T>&);
template <class T>
|
by: ypjofficial |
last post by:
Is there any possibility of invoking the member functions of a class
without creating an object (or even a pointer to ) of that class.
eg.
#include <iostream.h>
class test
{
public:
void...
|
by: utab |
last post by:
Dear all,
Is there a clear distinction how to decide which functions to be
members of a class and which not
How is your attitude (Your general way from your experiences ...)
"If the...
|
by: dascandy |
last post by:
Hello,
I was wondering, why is overloading operator. (period) forbidden? It
would make a few odd applications possible (dynamic inheritance and
transparent remote method invocation spring to my...
|
by: Tim Frink |
last post by:
Hi,
I'm experimenting with function pointers and found
two questions. Let's assume this code:
1 #include <iostream>
2 class A;
3
4 ////////////////////////////////////////////
5 class B
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |