2013-02-05 25 views
6

尝试把助推新协程功能,我创建了以下程序:加速协程断言失败

#include <boost/coroutine/all.hpp> 
#include <string> 
#include <vector> 


typedef boost::coroutines::coroutine<int(char)> coroutine_t; 


void f(coroutine_t::caller_type & ca) 
{ 
    std::vector<int> vec = {1, 2, 3}; 
    for (int i : vec) 
    { 
     char c = ca.get(); 
     std::cout << "c: " << c << std::endl; 
     ca(i); 
    } 
} 

int main() 
{ 
    coroutine_t cr(f); 
    std::string str("abc"); 
    for (char c : str) 
    { 
     std::cout << c << std::flush; 
     cr(c); 
     int n = cr.get(); 
     std::cout << n << std::endl;   
    } 
} 

的代码是基于sample code from the docs

我的生成命令去如下:

$ g++ -std=c++11 -o test -I/usr/local/include -L/usr/local/lib main.cpp /usr/local/lib/libboost_context.a 

输出:

$ ./test 
test: /usr/local/include/boost/coroutine/detail/coroutine_get.hpp:43: typename boost::coroutines::detail::param<Result>::type boost::coroutines::detail::coroutine_get<D, Result, arity>::get() const [with D = boost::coroutines::coroutine<char(int), 1>; Result = char; int arity = 1; typename boost::coroutines::detail::param<Result>::type = char]: Assertion `static_cast< D const* >(this)->impl_->result_' failed. 
Aborted (core dumped) 

该计划是由于失败的断言中止。你能帮我找到我的代码中的错误吗?

回答

2

我相信你需要在函数f的开头添加一个调用ca()

从升压文档:

执行控制转移施工(协程功能进入了),以协程 - 控制时,应返回到原来的调用程序,调用的boost ::协程::协程<> :: operator()在类型为boost :: coroutines :: coroutine的第一个参数<> :: caller_type在协程函数中。