- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在工作中的 C++ 项目中使用协程。但是,由于 callcc()
的使用不明确,它不会编译。我已经尝试在协程构造函数中指定分配器,但这似乎没有什么不同。
在将代码剥离到最基本的部分后,我最终用与 boost 文档中的斐波那契示例非常相似的东西重现了这个问题。
#include <iostream>
#include <boost/coroutine2/all.hpp>
/*
c++ -std=c++11 -o corofib corofib.cpp
*/
typedef boost::coroutines2::coroutine<unsigned long> coro_t;
coro_t::pull_type generator(
[](coro_t::push_type& sink) {
unsigned long first = 1L;
unsigned long second = 1L;
unsigned long answer;
sink(first);
sink(second);
while(1) {
answer = first + second;
first = second;
second = answer;
sink(answer);
};
}
);
int main(int argc, char* argv[]) {
generator();
std::cout << generator.get() << std::endl;
generator();
std::cout << generator.get() << std::endl;
generator();
std::cout << generator.get() << std::endl;
generator();
std::cout << generator.get() << std::endl;
}
我正在使用:
这是我收到的错误消息。我已经删除了“required from”通知以试图缩短这篇文章:
lear@delegato:~/lamu$ c++ -std=c++14 -o corofib corofib.cpp
In file included from /usr/local/include/boost/coroutine2/detail/coroutine.hpp:48:0,
from /usr/local/include/boost/coroutine2/coroutine.hpp:15,
from /usr/local/include/boost/coroutine2/all.hpp:10,
from corofib.cpp:2:
/usr/local/include/boost/coroutine2/detail/pull_control_block_cc.ipp: In instantiation of ‘boost::coroutines2::detail::pull_coroutine<T>::control_block::control_block(boost::context::preallocated, StackAllocator, Fn&&) [with StackAllocator = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = <lambda(boost::coroutines2::coroutine<long unsigned int>::push_type&)>; T = long unsigned int]’:
/usr/local/include/boost/coroutine2/detail/pull_control_block_cc.ipp:105:14: error: call of overloaded ‘callcc(const std::allocator_arg_t&, boost::context::preallocated&, boost::context::basic_fixedsize_stack<boost::context::stack_traits>&, boost::coroutines2::detail::pull_coroutine<T>::control_block::control_block(boost::context::preallocated, StackAllocator, Fn&&) [with StackAllocator = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = <lambda(boost::coroutines2::coroutine<long unsigned int>::push_type&)>; T = long unsigned int]::<lambda(boost::context::continuation&&)>)’ is ambiguous
});
^
/usr/local/include/boost/coroutine2/detail/pull_control_block_cc.ipp:105:14: note: candidates are:
In file included from /usr/local/include/boost/coroutine2/detail/pull_control_block_cc.hpp:14:0,
from /usr/local/include/boost/coroutine2/detail/coroutine.hpp:37,
from /usr/local/include/boost/coroutine2/coroutine.hpp:15,
from /usr/local/include/boost/coroutine2/all.hpp:10,
from corofib.cpp:2:
/usr/local/include/boost/context/continuation.hpp:469:1: note: boost::context::continuation boost::context::callcc(std::allocator_arg_t, StackAlloc, Fn&&, Arg ...) [with StackAlloc = boost::context::preallocated; Fn = boost::context::basic_fixedsize_stack<boost::context::stack_traits>&; Arg = {boost::coroutines2::detail::pull_coroutine<T>::control_block::control_block(boost::context::preallocated, StackAllocator, Fn&&) [with StackAllocator = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = <lambda(boost::coroutines2::coroutine<long unsigned int>::push_type&)>; T = long unsigned int]::<lambda(boost::context::continuation&&)>}]
callcc( std::allocator_arg_t, StackAlloc salloc, Fn && fn, Arg ... arg) {
^
/usr/local/include/boost/context/continuation.hpp:483:1: note: boost::context::continuation boost::context::callcc(std::allocator_arg_t, boost::context::preallocated, StackAlloc, Fn&&, Arg ...) [with StackAlloc = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = boost::coroutines2::detail::pull_coroutine<T>::control_block::control_block(boost::context::preallocated, StackAllocator, Fn&&) [with StackAllocator = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = <lambda(boost::coroutines2::coroutine<long unsigned int>::push_type&)>; T = long unsigned int]::<lambda(boost::context::continuation&&)>; Arg = {}]
callcc( std::allocator_arg_t, preallocated palloc, StackAlloc salloc, Fn && fn, Arg ... arg) {
^
/usr/local/include/boost/context/continuation.hpp:514:1: note: boost::context::continuation boost::context::callcc(std::allocator_arg_t, boost::context::preallocated, StackAlloc, Fn&&) [with StackAlloc = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = boost::coroutines2::detail::pull_coroutine<T>::control_block::control_block(boost::context::preallocated, StackAllocator, Fn&&) [with StackAllocator = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = <lambda(boost::coroutines2::coroutine<long unsigned int>::push_type&)>; T = long unsigned int]::<lambda(boost::context::continuation&&)>]
callcc( std::allocator_arg_t, preallocated palloc, StackAlloc salloc, Fn && fn) {
^
/usr/local/include/boost/context/continuation.hpp:457:1: note: boost::context::continuation boost::context::callcc(Fn&&, Arg ...) [with Fn = const std::allocator_arg_t&; Arg = {boost::context::preallocated, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, boost::coroutines2::detail::pull_coroutine<T>::control_block::control_block(boost::context::preallocated, StackAllocator, Fn&&) [with StackAllocator = boost::context::basic_fixedsize_stack<boost::context::stack_traits>; Fn = <lambda(boost::coroutines2::coroutine<long unsigned int>::push_type&)>; T = long unsigned int]::<lambda(boost::context::continuation&&)>}; <template-parameter-1-3> = void]
callcc( Fn && fn, Arg ... arg) {
我认为代码基本上是合理的:我只需要以某种方式消除歧义。
我可以说说这在 python 中是多么容易,但它可能会引起笑声。
最佳答案
关于c++ - 由于对 callcc 的调用不明确,Boost 协程无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769482/
我开始接触 Ruby 并遇到了一个以前从未见过的函数 -- callcc。 我已经了解了它的用途的一些一般概念,但是当我尝试编写一个示例时却得到了意想不到的结果。 require 'continuat
我认为 ContT 的正确类型应该是 newtype ContT m a = ContT {runContT :: forall r. (a -> m r) -> m r} 和其他控制运算符 shif
我试图弄清楚如何获得 R 的 callCC 函数,以便对函数进行短路评估,以便与 lapply 和 Reduce 等函数一起使用。 动机 这将使 Reduce 和 lapply 具有渐近效率 > O(
我无法理解以前 question 的答案.我希望对以下内容的解释能够澄清事情。以下示例来自 fpcomplete import Control.Monad.Trans.Class import Con
我的背景是 Javascript、Python 和一点 Haskell。我试图理解 callCC,有很多解释,但我找不到它们微不足道,我遇到了这个 https://www.cs.bham.ac.uk/
我正在尝试复习类幻灯片。该代码应该打印一次“早期工作”,然后打印两次“后期工作”(您可以设置后期工作的重复次数)。但是我想知道为什么这段代码不起作用,我该如何修改代码?从现在开始,代码将生成“稍后工作
我想学习一些关于 Continuation 的知识,使用 callcc 方法从一些文章中键入几个示例,但我遇到了错误: NoMethodError: undefined method `callcc'
继续寻求理解 ContT 和 friend 的意义。请考虑下面的( absurd 但说明性的)代码: v :: IO (Either String [String]) v = return $ Lef
我真的很难理解 callCC。我得到了延续的力量,我一直在我的一些项目中使用这个概念来创造很酷的概念。但是我从来不需要使用比 cont :: ((a->r)->r)-> Cont r a 更强大的东西
我正在尝试在工作中的 C++ 项目中使用协程。但是,由于 callcc() 的使用不明确,它不会编译。我已经尝试在协程构造函数中指定分配器,但这似乎没有什么不同。 在将代码剥离到最基本的部分后,我最终
让我们考虑突破一个非终止的折叠: (call/cc (lambda (folded) (stream-fold (lambda (acc v) (if ( foldl (\ac
我是一名优秀的程序员,十分优秀!