我试图理解,出于什么原因,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;
}