【问题标题】:C++ - how to write the type pointed to by a function pointerC++ - 如何编写函数指针指向的类型
【发布时间】:2016-02-23 14:23:35
【问题描述】:

函数指针指向的事物的类型怎么写?

换句话说,我可以得到它的类型:

#include <type_traits>
typedef std::remove_pointer<void(*)(int)>::type func;

但是如何在不使用std::remove_pointer&lt;&gt; 的情况下编写func 类型的名称?

【问题讨论】:

  • void func(int); 不工作吗?
  • 哈,是的,我的错。正在尝试typedef void(int) func;。意识到我应该在你发表评论的同时试试这个。
  • C++11 起,using func = void(int);.
  • @alain 这个声明难道不是声明一个 void function(不是 type!)func 采用 int 吗?
  • 是的,@PeterA.Schneider,这就是我没有将其作为答案发布的原因。但是因为类型最常见的用途是创建实例,所以我想它可能就是所有需要的。在函数类型(不是指针)的情况下,我必须承认我不知道任何其他用途......

标签: c++ templates types function-pointers template-meta-programming


【解决方案1】:

using:

using func = void(int);

typedef:

typedef void(func)(int);

其中一个肯定比另一个更好。

【讨论】:

  • 问题是哪一个:)
猜你喜欢
  • 1970-01-01
  • 2016-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多