为什么range

编程入门 行业动态 更新时间:2024-10-25 13:23:47
为什么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.

更多推荐

本文发布于:2023-07-20 12:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199653.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:range

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!