推导函数指针返回类型

Kor*_*icz 10 c++ function-pointers c++11

我认为代码会更好地说明我的需求:

template <typename F>
struct return_type
{
  typedef ??? type;
};
Run Code Online (Sandbox Code Playgroud)

以便:

return_type<int(*)()>::type -> int
return_type<void(*)(int,int)>::type -> void
Run Code Online (Sandbox Code Playgroud)

我知道的decltyperesult_of,但他们需要有传递的参数.我想从单个模板参数推断出函数指针的返回类型.我无法将返回类型添加为参数,因为这正是我想要隐藏的内容......

我知道有一个提升解决方案,但我不能使用它,并试图从提升中挖掘它导致了一个壮观的失败(通常如此).

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

Rei*_*ica 11

如果你可以使用可变参数模板(11月'12 CTP),这应该工作:

template <class F>
struct return_type;

template <class R, class... A>
struct return_type<R (*)(A...)>
{
  typedef R type;
};
Run Code Online (Sandbox Code Playgroud)

实例.

如果您不能使用可变参数模板,则必须为0,1,2,...参数(手动或预处理器生成)提供特定的特化.

编辑

正如评论中所指出的,如果你想使用可变参数函数,你将不得不添加一个额外的部分特化(或者在无变量模板的情况下为每个参数计数添加一个):

template <class R, class... A>
struct return_type<R (*)(A..., ...)>
{
  typedef R type;
};
Run Code Online (Sandbox Code Playgroud)