【问题标题】:C++ weak functor solutionC++弱函子解决方案
【发布时间】:2014-04-18 20:56:43
【问题描述】:

这是特定于 void() 函子的,但这对我来说很好......

struct Foo
{
   void Bar(int x)
   {
      std::cout << x << std::endl;
   }
};

struct VoidBind
{
    typedef void result_type;

    template<typename T> void operator()(const std::weak_ptr<T>& obj, std::function<void()>& func)
    {
        std::shared_ptr<T> shared_obj = obj.lock();

        if (shared_obj)
        {
            func();
        }
    }

    template<typename T> static std::function<void()> bind(const std::shared_ptr<T>& obj, const std::function<void()>& func)
    {
        return std::bind(VoidBind(), std::weak_ptr<T>(obj), func);
    }
};

#define BIND(F, O, A...)  VoidBind::bind(O, std::function<void()>(std::bind(F, O.get(), ##A)))

此代码随后被调用为...

auto obj = std::make_shared<Foo>();
auto func = BIND(&Foo::Bar, obj, 99);  // match std::bind style
func();
obj.reset();  // destroy object
func();       // will not do anything

我的问题是是否有某种方法可以避免 BIND 宏?

【问题讨论】:

    标签: c++ std-function stdbind


    【解决方案1】:

    这里可以使用可变参数模板:

    template<class F, class O, class... Args>
    auto bind(F f, O&& o, Args&&... args)
        -> decltype(VoidBind::bind(o, std::function<void()>(std::bind(f, O.get(), args...))))
    {
        return VoidBind::bind(O, std::function<void()>(std::bind(F, O.get(), args...)));
    }
    

    使用 C++14 自动返回类型推导会变得更好,您甚至不需要指定返回值:

    template<class F, class O, class... Args>
    auto bind(F f, O&& o, Args&&... args)
    {
        return VoidBind::bind(o, std::function<void()>(std::bind(f, O.get(), args...)));
    }
    

    【讨论】:

      【解决方案2】:

      您是否考虑过使用 lambda 表达式而不是仿函数对象 - 功能相同,但更紧凑,并且将绑定作为该 lambda 声明的一部分。

      【讨论】:

      • 很遗憾,我必须使用的 G++ 版本不支持 lambda 表达式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多