Boost logo

Boost :

Subject: [boost] [Function] Problem using target() with boost::mem_fn and boost::bind
From: Pengcheng Song (pengcheng.simon_at_[hidden])
Date: 2008-12-03 15:14:33

   I have encountered a crash trying to use the target() function to
retrieve a function pointer from a boost.function initialized with a member
function (using boost.bind). See the code below.

// ===========================================
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

typedef double(*FunctionType) (double);

double Square( FunctionType f, double x )
        return f(x) * f(x) ;

class A
        double x_;
        double y_;
        A() : x_(1.0), y_(1.0)

        double Area(double x, double y)
                x_ = x;
                y_ = y;
                return x_ * y_;

int main()
        A a1;
        boost::function<double(double)> f1 = boost::bind( boost::mem_fn(&A::Area),
&a1, _1, 2.5 );
        double s1 = f1(2.0); // this works
        FunctionType f2 = *<FunctionType>() ; // Error: this would crash
        s1 = Square( f2, 2.0 );
        return 0;
// ======================================================

After debugging the code, I found out that the crashed happened inside
target() function in file function_base.hpp:

  template<typename Functor>
    Functor* target()
      if (!vtable) return 0;

      detail::function::function_buffer type_result;
      type_result.const_obj_ptr = &typeid(Functor);
      vtable->manager(functor, type_result,
      return static_cast<Functor*>(type_result.obj_ptr);

After vtable->manager() call, type_result became null, and this caused
static_cast to fail. I was using boost_1_36_0 precompiled by BoostPro with
Visual Studio 2008.

Is this an expected behavior? Your feedback would be greatly appreciated.

Pengcheng Song

View this message in context:
Sent from the Boost - Dev mailing list archive at

Boost list run by bdawes at, gregod at, cpdaniel at, john at