2017-01-22 18 views
2

私はここですでに答えられた質問を見つけることができなかったのですが、lambdasを使わずにcoroutine2 libをうまく使用していれば助けになります。 私の問題、sumarized:lambdaなしのboost coroutine2の使用

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 
関連する問題