【问题标题】:Inferred return type when passing function by template通过模板传递函数时推断的返回类型
【发布时间】:2013-02-17 21:28:13
【问题描述】:

我的问题是让编译器根据模板传递的函数的返回类型推断函数的返回类型。

有什么方法可以调用

foo<bar>(7.3)

而不是

foo<double, int, bar>(7.3)

在这个例子中:

#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));
}

【问题讨论】:

  • 你可以做到foo(bar, 7.3)

标签: c++ templates function-pointers type-inference


【解决方案1】:

如果您想将bar 保留为模板 参数,恐怕您只能接近:

#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));
}

如果你想避免重复bar的名字,你也可以定义一个宏:

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

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

或者,您可以让bar 成为一个函数 参数,这可以让您的生活更轻松:

#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));
}

【讨论】:

  • 我的错误,std::result_of&lt;F(A)&gt; 是一种 hack,它会导致使用参数 A 调用类型为 F 的函数的结果,即使 F(A) 在技术上是函数的类型带有A 类型的参数并返回F。可怕的虐待。
  • 我发现了另一种方法:template&lt;typename F, typename T&gt; auto foo(F f, T t) -&gt; decltype(f(t)) { return f(t); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
相关资源
最近更新 更多