C++ 推导函数指针返回类型

C++ 推导函数指针返回类型,c++,c++11,function-pointers,C++,C++11,Function Pointers,我认为代码将更好地说明我的需要: template <typename F> struct return_type { typedef ??? type; }; 模板 结构返回类型 { typedef???型; }; 以便: return_type<int(*)()>::type -> int return_type<void(*)(int,int)>::type -> void return\u type::type->int 返回类型

我认为代码将更好地说明我的需要:

template <typename F>
struct return_type
{
  typedef ??? type;
};
模板
结构返回类型
{
typedef???型;
};
以便:

return_type<int(*)()>::type -> int
return_type<void(*)(int,int)>::type -> void
return\u type::type->int
返回类型::类型->无效
我知道的
decltype
result\u,但它们需要传递参数。我想从单个模板参数推断函数指针的返回类型。我无法将返回类型添加为参数,因为这正是我想要隐藏在这里的内容

我知道boost中有一个解决方案,但我不能使用它,而试图从boost中挖掘出来的结果是一个巨大的失败(就像它经常做的那样)


欢迎使用C++11解决方案(只要VS2012支持)。

如果您可以使用可变模板(11月12日CTP),这应该可以:

template <class F>
struct return_type;

template <class R, class... A>
struct return_type<R (*)(A...)>
{
  typedef R type;
};
模板
结构返回类型;
模板
结构返回类型
{
typedef-R型;
};

如果你不能使用可变模板,你必须为0,1,2。。。参数(手动或预处理器生成)

编辑

正如评论中所指出的,如果您还想使用可变函数,您必须添加一个额外的部分专门化(或在无可变模板的情况下,每个参数计数一个):

模板
结构返回类型
{
typedef-R型;
};

我不能使用CTP,我希望我能避免自己编写所有这些类型的痛苦。我不敢相信没有标准的解决方案…@KornelKisielewicz:如果你看一下boost,它确实做到了答案中的内容,但把它全部写出来了。您可以从那里复制并粘贴。正式地说,给定该模板适用的N个函数类型,这将错过
(3*N)*(2*N)*N
模板不适用的其他函数类型。例如,给定
void()const volatile&&
等等。@johanneschaub litb如何创建指向函数的指针(而不是指向成员函数的指针),该指针可以指向
const
限定函数或ref-qualified函数?@Angew OK这是一个很好的观点:)因此,在编写函数绑定时,您只会错过
N
函数类型:)“void”返回类型不同,因此我有一个专门用于
void
的模板。要使用它,我需要返回类型,我不想再次重新键入所有参数计数。
template <class R, class... A>
struct return_type<R (*)(A..., ...)>
{
  typedef R type;
};