Boost lambda bind and boost smart_ptr issue
Hi all, I have a problem involving Boost Lambda and shared_ptr. Although this has come up before, my problem is a little different. Essentially, I have classes derived from bases that contain pure virtuals. These are stored in STL containers as shared_ptrs (to the base) and to avoid having to write all those pesky functions/functors for STL algorithms, it would be nice to use Lambda instead. The following is similar to the sort of code I'm writing: class shape { public: virtual void draw(const int)=0; }; class rectangle : public shape { public: virtual void draw(const int x){std::cout << "rectangle::draw() with x = " << x << "\n";} }; class circle : public shape { public: virtual void draw(const int x) {std::cout << "circle::draw() with x = " << x << "\n";} }; vector< shared_ptr<shape> > shapes; // assume I stick stuff into shapes... for_each(shapes.begin(), shapes.end(), bind(&shape::draw, _1, 2)); The last statement fails under VC++ 2008 with error C2665: 'boost::lambda::function_adaptor<Func>::apply' : none of the 2 overloads could convert all the argument types This works if you use boost::bind instead of the lambda bind. The rest of the error message (too lengthy to list) seems to be telling me that there is an ambiguity between the two following types: Result boost::lambda::function_adaptor<Func>::apply<RET,A3>(Result (__thiscall shape::* )(Arg1),Object *,A1 &) Result boost::lambda::function_adaptor<Func>::apply<RET,A3>(Result (__thiscall shape::* )(Arg1),Object &,A1 &) I really could use a solution to this since it's stopping me from using lambda more often. Thanks in advance, Brian Martin
AMDG Brian Martin wrote:
class shape { public: virtual void draw(const int)=0; };
<snip>
vector< shared_ptr<shape> > shapes;
// assume I stick stuff into shapes...
for_each(shapes.begin(), shapes.end(), bind(&shape::draw, _1, 2));
The last statement fails under VC++ 2008 with error C2665: 'boost::lambda::function_adaptor<Func>::apply' : none of the 2 overloads could convert all the argument types
Dereferencing the pointer should work with the current trunk. In Christ, Steven Watanabe
Thanx for the reply. If by dereferencing the pointer you mean for_each(shapes.begin(), shapes.end(), bind(&shape::draw, *_1, 2)); then I get another error - cannot instantiate abstract class. Plus I would expect the dereferencing to slice off the derived bits of circle and rectangle. Regards Brian Martin On Wed, Jun 9, 2010 at 2:42 AM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Brian Martin wrote:
class shape { public: virtual void draw(const int)=0; };
<snip>
vector< shared_ptr<shape> > shapes;
// assume I stick stuff into shapes...
for_each(shapes.begin(), shapes.end(), bind(&shape::draw, _1, 2));
The last statement fails under VC++ 2008 with error C2665: 'boost::lambda::function_adaptor<Func>::apply' : none of the 2 overloads could convert all the argument types
Dereferencing the pointer should work with the current trunk.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
AMDG Brian Martin wrote:
Thanx for the reply. If by dereferencing the pointer you mean for_each(shapes.begin(), shapes.end(), bind(&shape::draw, *_1, 2));
then I get another error - cannot instantiate abstract class.
Yeah. I fixed this yesterday. There's also a possibility that &*_1 will work.
Plus I would expect the dereferencing to slice off the derived bits of circle and rectangle.
It won't, because the objects will be passed by reference. In Christ, Steven Watanabe
Ah - &*_1 works. Thanks. Regards, Brian Martin On Wed, Jun 9, 2010 at 4:00 PM, Steven Watanabe <watanabesj@gmail.com>wrote:
AMDG
Brian Martin wrote:
Thanx for the reply. If by dereferencing the pointer you mean for_each(shapes.begin(), shapes.end(), bind(&shape::draw, *_1, 2));
then I get another error - cannot instantiate abstract class.
Yeah. I fixed this yesterday. There's also a possibility that &*_1 will work.
Plus I would expect the dereferencing to slice off the derived bits of
circle and rectangle.
It won't, because the objects will be passed by reference.
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Brian Martin
-
Steven Watanabe