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

喜欢而已 提交于 2021-02-19 06:35:11

问题


Currently, I can compose ranges-v3 views like this:

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

But if I wanted to return v from a function I'd need to know its type. What is the type of a ranges-v3 view?


回答1:


Since C++14 you can use auto as the return type of functions and it will get deduced:

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)

The only downside is that the definition of f has to appear in the same TU where you are using it.



来源:https://stackoverflow.com/questions/53600644/how-do-you-declare-a-ranges-v3-view-return-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!