3

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

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

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

4

1 回答 1

5

从 C++14 开始,您可以将auto其用作函数的返回类型,它将被推导出:

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 中。

于 2018-12-03T19:43:37.893 回答