0

Since the ranges-library is up for inclusion into the standard I was toying around with it for a while and I have some problems with very basic concepts.

The toy example I am struggling with is this:

#include <iostream>
#include <range/v3/all.hpp>
using namespace ranges;


int main (int argc, char const* argv[]) {
    auto v = view::iota(1, 10);
    std::default_random_engine gen;
    auto t = action::push_back(view::iota(0, 10)) | action::shuffle(gen);
    std::cout << t << "\n";
}

The fact that it is not printable kind of suggests to me that t is not really a proper range. Unfortunately the documentation (where there is any) really abuses auto, so I do not know what is going on. What do I have to do, why do I have to do it (and in case anyone does know: how is the result type going to be documented (imo proxy objects and auto don't mix well - see the Eigen library as an example)).

My second question is related to the cargo-culted action::push_back. If I omit it I get a concept error telling me that the iota-generated view needs to be mutable. Obviously a view is by design immutable, so I get the problem here, but is "pushing nothing" really the preferred way to lift things into a mutable/stateful object or are there alternatives?

Best, Richard

4

1 回答 1

3

Your t instance is an action, not a range. That's the main reason why it isn't <<able.

You can see this by applying it to multiple containers, e.g.

#include <iostream>
#include <experimental/iterator>
#include <range/v3/all.hpp>
using namespace ranges;


int main (int argc, char const* argv[]) {
    std::default_random_engine gen;
    auto t = action::push_back(view::iota(0, 10)) | action::shuffle(gen);
    auto v1 = std::vector<int>{} | t;
    auto v2 = std::vector<int>{} | t;
    auto v3 = std::vector<int>{} | t;
    auto v4 = std::vector<int>{} | t;
    std::copy(v1.begin(), v1.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
    std::copy(v2.begin(), v2.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
    std::copy(v3.begin(), v3.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
    std::copy(v4.begin(), v4.end(), std::experimental::make_ostream_joiner(std::cout, ", "));
    std::cout << "\n";
}
于 2017-03-29T13:10:16.377 回答