如何使用语法使用 C++11 typedef 函数指针?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16498969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I typedef a function pointer with the C++11 using syntax?
提问by rubenvb
I'd like to write this
我想写这个
typedef void (*FunctionPtr)();
using using
. How would I do that?
使用using
. 我该怎么做?
回答by 0x499602D2
It has a similar syntax, except you remove the identifier from the pointer:
它具有类似的语法,但您从指针中删除了标识符:
using FunctionPtr = void (*)();
Here is an Example
这是一个例子
If you want to "take away the uglyness", try what Xeo suggested:
如果你想“去掉丑陋”,试试 Xeo 的建议:
#include <type_traits>
using FunctionPtr = std::add_pointer<void()>::type;
And here is another demo.
这是另一个演示。
回答by Vadim Goryunov
The "ugliness" can also be taken away if you avoid typedef-ing a pointer:
如果您避免对指针进行类型定义,也可以消除“丑陋”:
void f() {}
using Function_t = void();
Function_t* ptr = f;
ptr();
回答by Andrew Tomazos
You want a type-id
, which is essentially exactly the same as a declaration except you delete the declarator-id
. The declarator-id
is usually an identifier, and the name you are declaring in the equivilant declaration.
您需要 a type-id
,它本质上与声明完全相同,只是您删除了declarator-id
. 在declarator-id
通常是一个标识符,和名字你在equivilant声明宣布。
For example:
例如:
int x
The declarator-id
is x
so just remove it:
该declarator-id
是x
这样只是将其删除:
int
Likewise:
同样地:
int x[10]
Remove the x
:
删除x
:
int[10]
For your example:
对于您的示例:
void (*FunctionPtr)()
Here the declarator-id
is FunctionPtr
. so just remove it to get the type-id
:
这里declarator-id
是FunctionPtr
。所以只需将其删除即可获得type-id
:
void (*)()
This works because given a type-id
you can always determine uniquely where the identifier would go to create a declaration. From 8.1.1 in the standard:
这是有效的,因为给定 atype-id
您总是可以唯一地确定标识符将在哪里创建声明。从标准中的 8.1.1 开始:
It is possible to identify uniquely the location in the [type-id] where the identifier would appear if the construction were a [declaration]. The named type is then the same as the type of the hypothetical identifier.
如果构造是 [声明],则可以唯一标识 [type-id] 中标识符出现的位置。命名类型与假设标识符的类型相同。
回答by Leo Goodstadt
How about this syntax for clarity? (Note double parenthesis)
为了清楚起见,这个语法怎么样?(注意双括号)
void func();
using FunctionPtr = decltype((func));
回答by Silvester
Another approach might using auto return type with trailing return type.
另一种方法可能是使用带有尾随返回类型的自动返回类型。
using FunctionPtr = auto (*)(int*) -> void;
This has the arguable advantage of being able to tell something is a function ptr when the alias begins with "auto(*)" and it's not obfuscated by the identifier names.
当别名以“auto(*)”开头并且没有被标识符名称混淆时,这有一个有争议的优势,即能够告诉某个东西是一个函数 ptr 。
Compare
相比
typedef someStructureWithAWeirdName& (FunctionPtr*)(type1*, type2**, type3<type4&>);
with
和
using FunctionPtr = auto (*)(type1*, type2**, type3<type4&>) -> someStructureWithAWeirdName&;
Disclaimer: I took this from Bean Deane's "Easing into Modern C++" talk
免责声明:我摘自 Bean Deane 的“Easing into Modern C++”演讲