我可以累积到范围v3吗? [英] Can I pipe to range-v3 accumulate?

查看:119
本文介绍了我可以累积到范围v3吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从3年前开始发现了较旧的问题,这些问题通常说是不可能的,但是我真的想通过管道进行累积,因为在某些情况下它非常好,例如:

I found older questions from 3y ago that say that in general it is not possible, but I would really like to pipe to accumulate since it is quite nice in some cases, for example this:

const double val = data | transform(...) | accumulate (...);

所以我想知道是否在range-v3/C ++ 20范围中添加了一些使我能够做到这一点的东西.

So I wonder if something has been added to range-v3/C++20 ranges that enables me to do this.

推荐答案

否.

您只能插入范围适配器-范围内并产生范围的算法.在范围-v3或C ++ 20范围内, 不能采用可取范围并返回单个对象的算法(也称为变形").

The only things you can pipe into are range adapters -- algorithms that take in a range and produce a range. Algorithms that take in a range and return a single object (also known as catamorphisms) are not pipeable in range-v3 or C++20 ranges.

您必须这样写:

const double val = accumulate(data | transform(...));


关于为什么accumulate和类似算法将难以实现|的能力.考虑我们希望algo(rng, x)rng | algo(x)表示相同的意思.此外,请考虑全部通话" algo(rng, x)可以完全受限制(因为您拥有所有信息),而部分通话" algo(x)在除极少数情况下几乎必须不受所有限制...基本上是粗略地考虑auto&&...


As to why accumulate and similar algorithms will struggle to ever be |-able. Consider that we want algo(rng, x) and rng | algo(x) to mean the same thing. Further, consider that the "total call" algo(rng, x) can be fully constrained (since you have all the information) while the "partial call" algo(x) basically has to be completely unconstrained in all but rare circumstances... basically roughly taking auto&&...

问题是,当第二个参数x可以成为范围时,我们必然会陷入歧义.您如何区分是完全通话还是部分通话?

The problem is we necessarily run into ambiguities when the second argument, x, can also be a range. How do you distinguish between the intent being a total call or a partial call?

以下是使用string的示例:

accumulate("hello"s, ""s)

这是一个总调用,它使用默认的二进制运算符+-字符串连接.这样做是对char范围内的元素进行迭代,并将它们一个接一个地添加到初始空字符串中.这是一种复制string的低效但正确的方法.您最终得到的值是"hello"s.

This is a total call, that uses the default binary operator + - which is string concatentation. What this does is iterate over the elements of the range of chars and add them one by one to the initial empty string. This is an inefficient, yet correct, way to copy a string. You end up with the value "hello"s.

等效管道版本如何?

"hello"s | accumulate(""s)

右侧是什么意思?可以将accumulate(""s)视为总通话吗?是的,它可以!默认的第二个参数为char(),默认的第三个参数为plus(),可以正常工作,因此accumulate(""s)的值是整数0-使整个表达式格式错误,因为没有operator|(string, int).

What does the right-hand side mean? Can accumulate(""s) be considered a total call? Yes it can! The defaulted 2nd argument would be char() and the defaulted third argument would be plus(), this works fine, and so the value of accumulate(""s) is the integer 0 - making the whole expression ill-formed because there's no operator|(string, int).

您如何使用accumulate进行此项工作?

How do you make this work with accumulate?

这篇关于我可以累积到范围v3吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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