简体   繁体   English

我可以通过管道传输到 range-v3 累积吗?

[英]Can I pipe to range-v3 accumulate?

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:我发现 3 年前的旧问题说一般来说这是不可能的,但我真的很想累积,因为在某些情况下它非常好,例如:

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

So I wonder if something has been added to range-v3/C++20 ranges that enables me to do this.所以我想知道是否在 range-v3/C++20 范围中添加了一些东西,使我能够做到这一点。

No.不。

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.接受范围并返回单个对象的算法(也称为 catamorphisms)在 range-v3 或 C++20 范围内不可管道化。

You have to write it this way:你必须这样写:

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

As to why accumulate and similar algorithms will struggle to ever be |至于为何accumulate和类似的算法将努力永远| -able. -有能力的。 Consider that we want algo(rng, x) and rng | algo(x)考虑我们想要algo(rng, x)rng | algo(x) rng | algo(x) to mean the same thing. rng | algo(x)表示同样的事情。 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&&...此外,考虑到“总调用” algo(rng, x)可以被完全约束(因为你拥有所有信息)而“部分调用” algo(x)基本上必须在除极少数情况下完全不受约束。 . 基本上大致走auto&&...

The problem is we necessarily run into ambiguities when the second argument, x , can also be a range.问题是当第二个参数x可以是一个范围时,我们必然会遇到歧义。 How do you distinguish between the intent being a total call or a partial call?您如何区分意图是全部调用还是部分调用?

Here's an example using string :这是一个使用string的示例:

accumulate("hello"s, ""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 char s and add them one by one to the initial empty string.它的作用是遍历char范围内的元素,并将它们一一添加到初始空字符串中。 This is an inefficient, yet correct, way to copy a string .这是一种低效但正确的方法来复制string You end up with the value "hello"s .你最终得到值"hello"s

What about its equivalent pipe version?它的等效管道版本怎么样?

"hello"s | accumulate(""s)

What does the right-hand side mean?右手边是什么意思? Can accumulate(""s) be considered a total call?可以accumulate(""s)被认为是一个总调用? 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) .默认的第二个参数是char() ,默认的第三个参数是plus() ,这很好用,所以accumulate(""s)是整数0 - 使整个表达式格式错误,因为没有operator|(string, int)

How do you make this work with accumulate ?你如何使这个工作与accumulate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM