繁体   English   中英

这是否使用函数类型(非指针)的别名与此typedef相同

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM