How do I write a range pipeline that uses temporary containers?

前端 未结 6 2422
予麋鹿
予麋鹿 2020-11-30 02:09

I have a third-party function with this signature:

std::vector f(T t);

I also have an existing potentially infinite range (of the

6条回答
  •  Happy的楠姐
    2020-11-30 02:53

    I suspect it just can't. None of the views have any machinery to store temporaries anywhere - that's explicitly against the concept of view from the docs:

    A view is a lightweight wrapper that presents a view of an underlying sequence of elements in some custom way without mutating or copying it. Views are cheap to create and copy, and have non-owning reference semantics.

    So in order for that join to work and outlive the expression, something somewhere has to hold onto those temporaries. That something could be an action. This would work (demo):

    auto rng = src | view::transform(f) | action::join;
    

    except obviously not for src being infinite, and even for finite src probably adds too much overhead for you to want to use anyway.

    You would probably have to copy/rewrite view::join to instead use some subtly modified version of view::all (required here) that instead of requiring an lvalue container (and returning an iterator pair into it), allowed for an rvalue container that it would store internally (and returning an iterator pair into that stored version). But that's several hundred lines' worth of copying code, so seems pretty unsatisfactory, even if that works.

提交回复
热议问题