c++ - 访问 boost::function 参数

标签 c++ boost boost-bind

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

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

最佳答案

boost::function 删除实现类型,但如果你知道它,你可以转换为它;由于 boost::function 是按值比较的 (== !=),因此信息清晰可用。

看起来(来自 functionN 的 function_base 父类(super class))您可以通过以下方式获取实现对象:

f.target<concrete_functor_type>()

如果您提供了错误的具体类型,它将返回 NULL。

同样在 function_base 中(除了上面的目标方法之外可能没有帮助):

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 使您可以访问:

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

可以得到仿函数的 typeid:

  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) 仅对函数对象的引用、绑定(bind)(这是固定的)成员函数 ptrs 和自由函数有用,您没有绑定(bind)任何参数

关于c++ - 访问 boost::function 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1395312/

相关文章:

c++ - 语法高亮 VS2012 C++ 和 OpenCV,库

c++ - 如何从 Catch 记者中排除 printf 消息

c++11 - boost::odeint my_vector.cpp 示例中的段错误

c++ - 使用Boost PRNG制作一个巨大的随机数查找表

c++ - 使用 typedef – 基于类的模板参数 – 在类范围之外

c++ - 用C++点击鼠标

c++ - 生产中的 STL vector 和 Boost Multi Array

C++问题:boost::bind receive other boost::bind

c++ - 使用 boost::bind 传递比预期更多的参数安全吗?

c++ - C++ boost 绑定(bind)性能