I am trying to make a member function call to all objects returned by an iterator. mem_fn is giving me an error when trying to call get_pointer() for a reference object. There is no overload for get_pointer of a reference object. Is there some reason or should I be doing this differently. The long description is that I am iterating over a container of objects. I would like to make a member function call on each of these objects. Then I get an error that there is no matching function for get_pointer( obj ). I have solved this by providing my own get_pointer() overload specifically for obj to prevent any bad things from happening until I can found out what the real problem is. Here is some pseudo code to get things in the right orientation. class obj { public: void method(); }; class obj_iterator { public: obj& iterator::operator*() const { return obj_; } }; class obj_container { public: obj_iterator begin(); obj_iterator end(); } { ... obj_container c; foreach( c.begin(), c.end(), mem_fn( method ) ); } Thanks, ...Duane
I've tried trying to figure that out too. I think what we are looking for is an adapter. I think you want to base your target object either on a unary_function or binary_function, but you don't have to. And I think that you need to use binders. But I don't know how. -JMT -----Original Message----- From: Duane Murphy [mailto:duanemurphy@mac.com] Sent: Wednesday, January 23, 2002 4:36 PM To: Boost Users Subject: [Boost-Users] mem_fn and references? I am trying to make a member function call to all objects returned by an iterator. mem_fn is giving me an error when trying to call get_pointer() for a reference object. There is no overload for get_pointer of a reference object. Is there some reason or should I be doing this differently. The long description is that I am iterating over a container of objects. I would like to make a member function call on each of these objects. Then I get an error that there is no matching function for get_pointer( obj ). I have solved this by providing my own get_pointer() overload specifically for obj to prevent any bad things from happening until I can found out what the real problem is. Here is some pseudo code to get things in the right orientation. class obj { public: void method(); }; class obj_iterator { public: obj& iterator::operator*() const { return obj_; } }; class obj_container { public: obj_iterator begin(); obj_iterator end(); } { ... obj_container c; foreach( c.begin(), c.end(), mem_fn( method ) ); } Thanks, ...Duane Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com Your use of Yahoo! Groups is subject to the Yahoo! http://docs.yahoo.com/info/terms/ Terms of Service. [Non-text portions of this message have been removed]
From: "Duane Murphy"
I am trying to make a member function call to all objects returned by an iterator. mem_fn is giving me an error when trying to call get_pointer() for a reference object. There is no overload for get_pointer of a reference object. Is there some reason or should I be doing this differently.
[...]
Here is some pseudo code to get things in the right orientation.
class obj { public: void method(); };
class obj_iterator { public: obj& iterator::operator*() const { return obj_; } };
class obj_container { public: obj_iterator begin(); obj_iterator end(); }
{ ... obj_container c; foreach( c.begin(), c.end(), mem_fn( method ) ); }
Your code works, when modified appropriately. mem_fn recognizes obj&
references.
The following modification, however, does not work (which might be your
problem):
#include
--- At Thu, 24 Jan 2002 15:05:24 +0200, Peter Dimov wrote:
int main() { obj_container c; std::for_each( c.begin(), c.end(), boost::mem_fn( &obj::method ) ); }
&obj::method is actually of type void (obj_base::*) (), and while mem_fn recognizes references to the 'base' type (obj_base&), it cannot tell whether obj& is a smart pointer or an object reference.
I can probably fix this by using boost::is_convertible but unfortunately it's known to break some compilers. Food for thought. Thanks for the bug report. :-)
Thank you for the explaination Peter. Your change was a translation problem on my part. Apologies. Is there an appropriate work around? I have been using: namespace boost { inline obj* get_pointer( obj & p ) { return &p; } } As this was the source of the compiler error. Is there a better work around? I am leary of a full template replacement for this because I dont know what the side affects would be: namespace boost { template < class T > inline T* get_pointer( T & r ) { return &r; } } ...Duane
From: "Duane Murphy"
Is there an appropriate work around? I have been using:
namespace boost { inline obj* get_pointer( obj & p ) { return &p; } }
As this was the source of the compiler error. Is there a better work around?
No, this is it - at the moment. You shouldn't need many of these, hopefully.
I am leary of a full template replacement for this because I dont know what the side affects would be:
namespace boost { template < class T > inline T* get_pointer( T & r ) { return &r; } }
This will break a lot of things. :-)
As this was the source of the compiler error. Is there a better work around?
No, this is it - at the moment. You shouldn't need many of these, hopefully.
Actually another workaround is to redefine obj_base::method in obj as well as a simple forwarding function, if you can live with the performance. I'm working on a fix.
participants (3)
-
Duane Murphy
-
Jack M. Thompson
-
Peter Dimov