Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Non-const "member functor" problem

Reply
Thread Tools

Non-const "member functor" problem

 
 
Lionel B
Guest
Posts: n/a
 
      12-01-2006
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 F> double 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 F> double 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
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-01-2006

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 F> double 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 F> double 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


 
Reply With Quote
 
 
 
 
Lionel B
Guest
Posts: n/a
 
      12-01-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57