Inferred return type when passing function by template

五迷三道 提交于 2019-12-23 14:53:38

问题


My question is about having the compiler infer the return type of a function based on the return type of a function passed by template.

Is there some way I can call as

foo<bar>(7.3)

instead of

foo<double, int, bar>(7.3)

in this example:

#include <cstdio>
template <class T, class V, V (*func)(T)>
V foo(T t) { return func(t); }

int bar(double j)  { return (int)(j + 1); }

int main() {
  printf("%d\n", foo<double, int, bar>(7.3));
}

回答1:


If you want to keep bar as a template argument, I'm afraid you can only get close to that:

#include <cstdio>

template<typename T>
struct traits { };

template<typename R, typename A>
struct traits<R(A)>
{
    typedef R ret_type;
    typedef A arg_type;
};

template <typename F, F* func>
typename traits<F>::ret_type foo(typename traits<F>::arg_type t)
{ return func(t); }

int bar(double j)  { return (int)(j + 1); }

int main()
{
    printf("%d\n", foo<decltype(bar), bar>(7.3));
}

You could also define a macro if you want to avoid repeating bar's name:

#define FXN_ARG(f) decltype(f), f

int main()
{
    printf("%d\n", foo<FXN_ARG(bar)>(7.3));
}

Alternatively, you could let bar become a function argument, which could make your life easier:

#include <cstdio>

template<typename T>
struct traits { };

template<typename R, typename A>
struct traits<R(A)>
{
    typedef R ret_type;
    typedef A arg_type;
};

template<typename R, typename A>
struct traits<R(*)(A)>
{
    typedef R ret_type;
    typedef A arg_type;
};

template <typename F>
typename traits<F>::ret_type foo(F f, typename traits<F>::arg_type t)
{ return f(t); }

int bar(double j)  { return (int)(j + 1); }

int main()
{
    printf("%d\n", foo(bar, 7.3));
}


来源:https://stackoverflow.com/questions/15190605/inferred-return-type-when-passing-function-by-template

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