[英]Is this using alias for a function type (not pointer) the same as this typedef
这些都一样吗?
using on_receipt = void (const string);
typedef void on_receipt(const string);
这些不是函数指针的别名,而是实际的函数类型。 他们都编译得很好。 奇怪的是, typedef
版本至少有函数名称的占位符,但由于using
在=
之前移动了这个占位符,因此返回和参数之间没有任何区别,看起来它可能是错误的或误导性的。
对,他们是:
[dcl.typedef]
2别名声明也可以引入typedef-name。
using
关键字后面的标识符变为typedef-name,并且该标识符后面的可选attribute-specifier-seq属于该typedef-name。 它具有与typedef
说明符引入的语义相同的语义。 特别是,它没有定义新类型。
#include <string>
#include <type_traits>
int main()
{
using on_receipt1 = void (const std::string);
typedef void on_receipt2(const std::string);
static_assert(std::is_same<on_receipt1, on_receipt2>::value, "");
}
你想错了。 on_receipt
不是“函数名称的占位符”,它是您引入的类型名称。 考虑一下使用情况。
你会像on_receipt function_variable;
一样使用它on_receipt function_variable;
,所以你的解释实际上是误导性的,因为你引用你的函数的“名称”不是on_receipt
而是function_variable
。
我会说函数的typedef
语法相当奇怪。 在using
样式时,您在一侧具有类型名称,而在另一侧具有定义它的事物。 在typedef
样式中,您将它交错。
typedef
有点看起来像一个函数声明,但它确实不是; 这是一种类型声明。 我觉得很乱。
哦,当然,这些陈述是等价的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.