繁体   English   中英

为什么传递非静态成员函数会导致编译错误?

[英]Why does passing a non-static member function cause compilation errors?

当我尝试调用下面的构造函数,向它传递一个静态成员函数时,我没有收到任何错误,但是当我向它传递一个非静态成员函数时,我收到一个编译错误:

构造函数

template <class callable, class... arguments>
Timer(int after, duration_type duration, bool async, callable&& f, arguments&&... args)
{

    std::function<typename std::result_of<callable(arguments...)>::type()> 
            task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));

}

调用

Timer timer(252222, duration_type::milliseconds, true, &MotionAnalyser::ObjectGarbageCollector); // Does not work because it does not point to object too.

Timer timer(252222, duration_type::milliseconds, true, std::bind(this, &MotionAnalyser::ObjectGarbageCollector)); //Should work, but does not?!?!

错误

Error   C2039   'type': is not a member of 'std::result_of<callable (void)>'    

到目前为止,我有:

  • std:function的使用方式,结果证明它与可调用类型结合使用,调用对象应该是可调用类型,因为我过度使用()运算符(基于我对可调用类型的理解)。
  • 我研究过将非静态成员函数传递给函数,因此我尝试使用std::bind
  • 谷歌搜索有关编译错误的有用信息。

您可以调用向后bind ,它首先需要可调用对象(在这种情况下是指向成员函数的指针),然后是参数。

std::bind(&MotionAnalyser::ObjectGarbageCollector, this)

但是,查看Timer的构造函数,您应该能够传递这些参数,因为它们无论如何都会被绑定:

Timer timer(252222, duration_type::milliseconds, true,
            &MotionAnalyser::ObjectGarbageCollector, this);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM