Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-01-24 08:05:24


From: "Duane Murphy" <duanemurphy_at_[hidden]>
> 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 <boost/mem_fn.hpp>
#include <algorithm>

class obj_base
{
public:
    void method();
};

class obj: public obj_base
{
};

class obj_iterator
{
public:
    obj& operator*() const;
    obj_iterator & operator++();
};

bool operator==(obj_iterator, obj_iterator);
bool operator!=(obj_iterator, obj_iterator);

class obj_container
{
public:
    obj_iterator begin();
    obj_iterator end();
};

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<U, T> but
unfortunately it's known to break some compilers. Food for thought. Thanks
for the bug report. :-)

--
Peter Dimov
Multi Media Ltd.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net