我已经在 scala 的平面地图上实现了一个函数,我想知道我是否可以访问 lambda 中的 lambda 的返回类型以避免在使用时重复它
/**
* Inspired on scala's flat map, provide a @param func which output will be flattened in the output
* sequence, which is the return type of @param func
*/
template <typename IN, typename F>
auto flat_mapf(const IN& input, F func)
-> decltype(func(std::declval<typename IN::value_type>()))
{
decltype(func(std::declval<typename IN::value_type>())) output;
auto outit = std::back_inserter(output);
for (auto i = input.begin(); i != input.end(); ++i)
{
decltype(func(std::declval<typename IN::value_type>())) interm = func(*i);
std::move(interm.begin(), interm.end(), outit);
}
return output;
}
// usage example, I would like to avoid repeating vector<size_t> type two times:
auto vo = flat_mapf(vi, [](const size_t& x) -> vector<size_t> {
vector<size_t> res;
for (size_t i = 0; i < x; ++i)
res.push_back(x);
return res;
});