提高::从lambda函数构造function_output_iterator不可转让 [英] boost::function_output_iterator constructed from lambda function is not assignable

查看:119
本文介绍了提高::从lambda函数构造function_output_iterator不可转让的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code片断:

Consider the following code snippet:

auto f = [](int x) { std::cout << x; };
auto it = boost::make_function_output_iterator(f);
decltype(it) it2 = it;  // Ok, copied
it2 = it;  // Does not compile, cannot assign!

问题是, function_output_iterator 以这种方式构建的不可转让,因而不能满足的迭代器概念,这需要键入要的 CopyAssignable

The problem is, function_output_iterator constructed in this way is not assignable, and thus does not satisfy the Iterator concept, which requires type to be CopyAssignable.

这是不是一个错误,因为升压功能输出迭代器文档明确 <一个href=\"http://www.boost.org/doc/libs/1_60_0/libs/iterator/doc/function_output_iterator.html#function-output-iterator-requirements\"相对=nofollow>说:

This is not a bug, since boost Function Output Iterator documentation clearly says:

UnaryFunction必须赋值和可复制构造的。

UnaryFunction must be Assignable and Copy Constructible.

虽然 lambda函数被删除:

ClosureType& operator=(const ClosureType&) = delete;

所以,这种行为在技术上是正确的,但对我来说是有些出人意料。我认为这是一个完全合理的愿望,构建 function_output_iterator 鉴于lambda函数产生的闭包。这似乎不方便我为什么这种使用情况会导致出现问题。

So this behaviour is technically correct, but for me is somewhat unexpected. I think it is a perfectly reasonable desire to construct function_output_iterator given a closure produced by lambda function. It seems inconvenient to me why this use case causes a problem.

嗯,好吧,这个计算器,所以我必须要问一些问题:)这就是:如何解决这个?如何获取给定一个封闭一个正确的迭代器,它的作用就像 function_output_iterator

Hm, ok, this StackOverflow, so I have to ask some question :) Here it is: how to workaround this? How to obtain a correct iterator given a closure, which acts like function_output_iterator?

和另外一个:不值得做一个提案或bug报告提振

And another one: does it worth to make a proposal or a bug report to boost?

推荐答案

只需保存在的std ::功能关闭

std::function<void(int)> f = [](int x) { std::cout << x; };
auto it = boost::make_function_output_iterator(f);

测试片段。

这篇关于提高::从lambda函数构造function_output_iterator不可转让的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆