0

是否可以访问 boost::function 类型中包含的参数?

我希望能够检索要调用的函数的地址,以及为该函数提供的参数的值。

4

1 回答 1

2

boost::function erases the implementation type, but if you know it, you can cast to it; since boost::function are comparable by value (== !=) the information is clearly available.

It looks like (from the function_base superclass of functionN) you can get the implementation object with:

f.target<concrete_functor_type>()

Which will return NULL if you provided the wrong concrete type.

Also in function_base (probably not helpful beyond the target method above):

public: // should be protected, but GCC 2.95.3 will fail to allow access
  detail::function::vtable_base* vtable;
  mutable detail::function::function_buffer functor;

vtable gives you access to:

      struct vtable_base
      {
        void (*manager)(const function_buffer& in_buffer, 
                        function_buffer& out_buffer, 
                        functor_manager_operation_type op);
      };

which can get you the typeid of the functor:

  case get_functor_type_tag:
    out_buffer.type.type = &typeid(F);
    out_buffer.type.const_qualified = in_buffer.obj_ref.is_const_qualified;
    out_buffer.type.volatile_qualified = in_buffer.obj_ref.is_volatile_qualified;
    return;
  }

function_buffer (functor) is only useful for refs to function objects, bound (this is fixed) member functions ptrs, and free functions, where you haven't bound any arguments

于 2009-09-08T17:54:35.250 回答