Search code examples
c++range-v3

why does range-v3 yield require default constructor


I am trying to understand, for what reasons does the yield family of functions require that class be default constructible?

In the following example, the vnums1 line compiles only if CNum has a default constructor. The vnums2 line does not require a default constructor.

I am using Visual Studio 2017 and Range-V3-VS2015. Thank you!

#include <range/v3/all.hpp>

struct CNum
{
    // CNum() = default;
    explicit CNum(int num) : m_num(num) {}
    int m_num;
};

int main()
{
    auto ints = ranges::view::ints(0, 10);

    // this compiles only of CNum has a default constructor
    auto vnums1 = ints
        | ranges::view::for_each([](int num) { return ranges::yield_if(num % 2, CNum(num)); })
        | ranges::to_vector;

    // this compiles even if CNum does not have a default constructor
    auto vnums2 = ints
        | ranges::view::remove_if([](int num) { return num % 2 == 0; })
        | ranges::view::transform([](int num) { return CNum(num); })
        | ranges::to_vector;

    return 0;
}

Solution

  • We just changed the code to not require DefaultConstructible. git pull and enjoy.