2017-01-22 59 views
2

我想這是我第一次在這裏找不到已經回答了的問題,如果有人成功使用boost coroutine2 lib而沒有使用lambdas,我真的可以使用一些幫助。 我的問題,sumarized:使用升級協程2而不使用lambdas

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> > > >&)’ 

有人能擺脫任何光線在這個問題?

回答

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