【问题标题】:Use of boost coroutine2 without lambdas使用没有 lambda 的 boost coroutine2
【发布时间】:2017-01-22 20:46:02
【问题描述】:

我想这是我第一次在这里找不到已经回答的问题,如果有人成功使用了没有 lambda 的 boost coroutine2 lib,我真的可以使用一些帮助。 我的问题,总结:

class worker {
...
void import_data(boost::coroutines2::coroutine
<boost::variant<long, long long, double, std::string> >::push_type& sink) {
...
sink(stol(fieldbuffer));
...
sink(stod(fieldbuffer));
...
sink(fieldbuffer); //Fieldbuffer is a std::string
}
};

我打算将它用作另一个类中的协程,它的任务是将每个产生的值放在它的位置,所以我尝试实例化一个对象:

worker _data_loader;
boost::coroutines2::coroutine<boost::variant<long, long long, double, string>>::pull_type _fieldloader
        (boost::bind(&worker::import_data, &_data_loader));

但这不会编译:

/usr/include/boost/bind/mem_fn.hpp:342:23:
error: invalid use of non-static member function of type 
‘void (worker::)(boost::coroutines2::detail::push_coroutine<boost::variant<long int, long long int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)’

有人能解释一下这个问题吗?

【问题讨论】:

    标签: c++ boost boost-bind boost-coroutine2


    【解决方案1】:

    这与 Boost Coroutine 无关。

    这只是与成员函数绑定。你忘了暴露未绑定的参数:

    boost::bind(&worker::import_data, &_data_loader, _1)
    

    Live On Coliru

    #include <boost/coroutine2/all.hpp>
    #include <boost/variant.hpp>
    #include <boost/bind.hpp>
    #include <string>
    
    using V    = boost::variant<long, long long, double, std::string>;
    using Coro = boost::coroutines2::coroutine<V>;
    
    class worker {
      public:
        void import_data(Coro::push_type &sink) {
            sink(stol(fieldbuffer));
            sink(stod(fieldbuffer));
            sink(fieldbuffer); // Fieldbuffer is a std::string
        }
    
        std::string fieldbuffer = "+042.42";
    };
    
    #include <iostream>
    int main() 
    {
        worker _data_loader;
        Coro::pull_type _fieldloader(boost::bind(&worker::import_data, &_data_loader, _1));
    
        while (_fieldloader) {
            std::cout << _fieldloader.get() << "\n";
            _fieldloader();
        }
    }
    

    打印

    42
    42.42
    +042.42
    

    【讨论】:

      猜你喜欢
      • 2019-07-31
      • 1970-01-01
      • 2018-02-26
      • 2016-12-23
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      相关资源
      最近更新 更多