Get return value for template lambda parameter, how to simplify code?

瘦欲@ 提交于 2019-12-07 19:43:03

问题


This is my trick:

template<typename F, typename TArg>
auto get_return_value(F * f = NULL, TArg * arg = NULL)
     -> decltype((*f)(*arg));

Example of using:

template<typename F, typename T>
decltype(get_return_value<F,T>()) applyFtoT(F f, T t)
{
    return f(t);
}

In case F is lambda:

int b = applyFtoT([](int a){return a*2}, 10);
// b == 20

Function get_return_value looks ugly i think... How to simplify it?


回答1:


It seems like you could eliminate the need for get_return_value by changing the declaration of applyFtoT like so:

template<typename F, typename T>
auto applyFtoT(F f, T t) -> decltype(f(t))
{
   return f(t);
}


来源:https://stackoverflow.com/questions/8462475/get-return-value-for-template-lambda-parameter-how-to-simplify-code

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