Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按模板传递函数时推断的返回类型_C++_Templates_Function Pointers_Type Inference - Fatal编程技术网

C++ 按模板传递函数时推断的返回类型

C++ 按模板传递函数时推断的返回类型,c++,templates,function-pointers,type-inference,C++,Templates,Function Pointers,Type Inference,我的问题是让编译器根据模板传递的函数的返回类型推断函数的返回类型 有什么办法我可以打电话给你吗 foo<bar>(7.3) foo(7.3) 而不是 foo<double, int, bar>(7.3) foo(7.3) 在本例中: #include <cstdio> template <class T, class V, V (*func)(T)> V foo(T t) { return func(t); } int bar(doubl

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

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

foo<bar>(7.3)
foo(7.3)
而不是

foo<double, int, bar>(7.3)
foo(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));
}
#包括
模板
V foo(T){return func(T);}
int条(双j){return(int)(j+1);}
int main(){
printf(“%d\n”,foo(7.3));
}

如果您想将
作为模板参数,恐怕您只能接近这一点:

#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
成为函数参数,这可以让您的生活更轻松:

#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));
}
#包括
模板
结构特征{};
模板
结构特征
{
类型def R ret_类型;
类型定义为arg_类型;
};
模板
结构特征
{
类型def R ret_类型;
类型定义为arg_类型;
};
模板
typename特征::ret_type foo(F,typename特征::arg_type t)
{返回f(t);}
int条(双j){return(int)(j+1);}
int main()
{
printf(“%d\n”,foo(bar,7.3));
}

你可以把它改成
foo(bar,7.3)
。我的错误是,
std::result\u of
是一种黑客行为,它产生的结果是带有
F
类型的函数的结果,用参数
a
调用,尽管
F(a)
从技术上讲是带有
a
类型参数的函数的类型,并且返回
F
。我发现了另外一种方法:
template auto-foo(F,T)->decltype(F(T)){return F(T);}
#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));
}