您如何声明 ranges-v3 视图 return 值?

How do you declare a ranges-v3 view return value?

目前,我可以像这样编写 ranges-v3 视图:

auto v = ranges::view::reverse | ranges::view::filter([](int l){return l>5;});

但是如果我想从函数中 return v,我需要知道它的类型。 ranges-v3 视图的类型是什么?

从 C++14 开始,您可以使用 auto 作为 return 类型的函数,它会被推导:

auto f() {
    return ranges::view::reverse | ranges::view::filter([](int l){return l>5;});
}
// f's return type is the type of the return expression, exactly as is I had:
// auto returnValue = return-expression;
// where f's type is decltype(returnValue)

唯一的缺点是 f 的定义必须出现在您使用它的同一个 TU 中。