8

After a career diversion, I am trying to get up to speed with std::views (and functional programming in general). I am using the '|' (pipe) operator with std::views::filter on a vector, and I am puzzled why some code structures compile and others don't.

This code creates a vector of vectors of int, then filters them by sum. I've commented the three statements that are confusing me, the first two of which compile and the third doesn't.

Compilation error is:

'|': no operator found which takes a left-hand operand of type 'std::vector<std::vector<int,std::allocator<int>>,std::allocator<std::vector<int,std::allocator<int>>>>' (or there is no acceptable conversion)  

(Using MSVC19, compiled with /std:c++latest)

I am puzzled as to why this doesn't compile while (2) especially does?

#include <vector>
#include <numeric>
#include <ranges>

template<typename T>
auto buildMultiples(const std::vector<T>& base)
{
    std::vector<std::vector<T>> vRet;
    for(T n= 1; n <= 5; n++)
    {
        auto v = base;
        for (auto& m : v) m *= n;
        vRet.push_back(v);
    }
    return vRet;
}

template<typename T>
struct sumGreaterThan
{
    T _limit{ 0 };

    auto operator()(const std::vector<T>& v) {return std::accumulate(v.cbegin(), v.cend(), 0) > _limit;}
};
   
int main()
{
    using namespace std;
    vector<int> nums{1,2,3,4,5,6,7,8,9};

    auto mults = buildMultiples(nums);

    for (auto& m : buildMultiples(nums)) {} //1. Compiles

    sumGreaterThan sumFilter{ 10 };
    
    auto vecs = buildMultiples(nums);
    for (auto& m : vecs | views::filter(sumFilter)) {} //2. Compiles

    for (auto& m : buildMultiples(nums) | views::filter(sumFilter)) {} //3. Compilation Error!!

    for (auto vecs = buildMultiples(nums); auto & m : vecs | views::filter(sumFilter)) {} // 4. Compiles. Thanks @Aryter
}
3
  • What if you use const auto&?
    – paddy
    Commented Dec 1, 2021 at 22:06
  • 1
    You can also do for (auto vecs = buildMultiples(nums); auto& m : vecs | views::filter(sumFilter)) if you want
    – Artyer
    Commented Dec 1, 2021 at 22:48
  • @Artyer That syntax looked weird to me (I wondered if you had mistyped a ; for a ,) until further research showed it only became valid in C++20. Thanks for suggesting!
    – DS_London
    Commented Dec 2, 2021 at 9:01

1 Answer 1

8

This is passing an lvalue vector into filter:

vecs | views::filter(sumFilter)

whereas this is passing an rvalue vector into filter:

buildMultiples(nums) | views::filter(sumFilter)

The current rule, which compilers implement, is that range adaptor pipelines cannot take rvalue non-view ranges (like vector, string, etc.). This is because the pipeline itself is non-owning (views were non-owning), and exists as a safety mechanism to prevent dangling.

The new rule, recently adopted as a defect, would allow this could and would cause filter to own the result of buildMultiples (this is P2415), but compilers don't implement it quite yet. With this change, your other version would also have compiled.

So for now, you will have to keep writing it this way (as you are already doing):

auto vecs = buildMultiples(nums);
for (auto& m : vecs | views::filter(sumFilter)) { ... }
3
  • I suspected it was an R-value, L-value thing, but I thought the result of buildMultiples() would be valid for the duration of the loop, as it is in version (1). The wording of the error description was a bit confusing too as version(2) seemed to be supplying the same lhs parameter type). Thank you for the clear explanation, and the update on the language evolution.
    – DS_London
    Commented Dec 2, 2021 at 9:07
  • 1
    @DS_London It would not have been if it were simply captured by reference - not all expressions in the range-based for initializer have their lifetime extended. Without the view actually owning the result of buildMultiples, your choice is either (a) don't compile or (b) have a dangling reference. Given that choice, I'd prefer (a). Although I even more strongly prefer option (c): just make it do the right thing. Which is what we did :-)
    – Barry
    Commented Dec 2, 2021 at 17:34
  • @DS_London To make it more confusing. ranges::for_each(buildMultiplies(~) | views::filter(~), [&](auto& m){ ... }) would have actually been just fine - it's specifically the range-based for that would unconditionally dangle :-(
    – Barry
    Commented Dec 2, 2021 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.