小编haz*_*dev的帖子

通过模板传递函数时推断的返回类型

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

有什么办法我可以打电话给

foo<bar>(7.3)
Run Code Online (Sandbox Code Playgroud)

代替

foo<double, int, bar>(7.3)
Run Code Online (Sandbox Code Playgroud)

在这个例子中:

#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));
}
Run Code Online (Sandbox Code Playgroud)

c++ templates type-inference function-pointers

5
推荐指数
1
解决办法
1060
查看次数