Boost logo

Boost Users :

From: Jaakko Järvi (jarvi_at_[hidden])
Date: 2006-11-30 23:39:16


Hello Haroon,

On Nov 30, 2006, at 12:37 AM, Haroon Khan wrote:

> Jaakko,
> Thanks for your reply, I tried some of your suggestions and they are
> still giving me compile errors. Here is some code that Ive been
> playing with. This was compiled in VC8.
>
> struct some_functor
> {
> private:
> int x_;
> public:
> template<typename SOMETYPE>
> some_functor& template_foo(SOMETYPE x){
> std::cout<<boost::format("Calling %x.%s(%d)\n") % this %
> __FUNCTION__%x;
> return *this;
> }
> };

> void test16
> {
> PRINT_FUNCTION_NAME;
> using namespace boost::lambda;
> int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
> std::vector<int> v(a, a+sizeof(a)/sizeof(int));
>
>
> (bind(&(some_functor::template_foo<int>), some_functor(),
> _1))(boost::cref(1))
>

The arguments given to bind at the time of call to bind (e.g.
some_functor) are stored as
const copies. Thus some_functor will be const, and when the functor
is called, you are trying to
call a non-const member with a const object.

Here's one way to fix it:

     some_functor s;
     (bind(&some_functor::template_foo<int>, boost::ref(s),
          _1))(boost::cref(1));

Cheers, Jaakko


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