5
Ranges (C++20) (euroquis.nl)
submitted 1 year ago by thomask to c/cpp@programming.dev
top 1 comments
sorted by: hot top controversial new old
[-] cgtjsiwy@programming.dev 3 points 1 year ago

In C++23, you can zip the two arrays, so there's no need to declare an otherwise useless index variable:

int stdSumOfValidDataPoints(const MyData& data)
{
    using namespace std;
    auto&& r = ranges::views::zip_transform(
        [](int data, bool valid){return data * valid;},
        data.dataPoint,
        data.dataPointIsValid
    );
    return accumulate(begin(r), end(r), 0);
}

Unfortunately, the current implementation of zip_transform in GCC doesn't understand that the output length is constant 8, so it does needless cmps every iteration to check if it should stop iterating. (Godbolt)

this post was submitted on 25 Jun 2023
5 points (100.0% liked)

C++

1732 readers
3 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS