为什么range-v3 yield需要默认构造函数(why does range-v3 yield require default constructor)
我试图理解,由于什么原因,yield函数族要求该类是默认的可构造的?
在以下示例中,仅当CNum具有默认构造函数时,vnums1行才会编译。 vnums2行不需要默认构造函数。
我使用的是Visual Studio 2017和Range-V3-VS2015。 谢谢!
#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; }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; }最满意答案
我们刚刚将代码更改为不需要DefaultConstructible。 git拉,享受。
We just changed the code to not require DefaultConstructible. git pull and enjoy.
更多推荐
发布评论