typedef和C ++ 11类型别名之间的区别 [英] Difference between typedef and C++11 type alias
问题描述
我在这里阅读模板别名: http://en.cppreference.com/ w / cpp / language / type_alias
I'm reading template aliases here: http://en.cppreference.com/w/cpp/language/type_alias
我想知道,即使它写在我链接的页面的第一行,有什么区别在typedef和类型别名之间(使用mytype = T;
)
And I'm wondering, even if it's written on the very first line of the page I linked, what's the difference between a typedef and a type alias (using mytype = T;
)
它们不是可互换的吗?
推荐答案
两者之间绝对没有区别。
如果您可以看一下标准:
If you take a look at the standard :
7.1.3 typedef规范[dcl.typedef]
一个 typedef-name 也可以通过 alias-declaration 引入。 using
关键字之后的 identifier 成为 typedef-name 。 它的语义与 typedef
说明符引入的语义相同。尤其是,它没有定义新的类型,因此不会出现在 type-id 中。
A typedef-name can also be introduced by an alias-declaration. The identifier following the using
keyword becomes a typedef-name. It has the same semantics as if it were introduced by the typedef
specifier. In particular, it does not define a new type and it shall not appear in the type-id.
7.3.3使用声明[namespace.udecl]
如果 using-declaration 使用关键字 typename 并指定一个从属名称(14.6.2),即 using-declaration引入的名称被视为 typedef-name 。
If a using-declaration uses the keyword typename and specifies a dependent name (14.6.2), the name introduced by the using-declaration is treated as a typedef-name.
但是从此页面:< a href = http://en.cppreference.com/w/cpp/language/type_alias rel = noreferrer> http://en.cppreference.com/w/cpp/language/type_alias
据说:
类型别名类似于 typedefs ,但是,它具有使用模板的优势。
这似乎是
// template type alias
template<class T> using ptr = T*;
// the name 'ptr<T>' is now an alias for pointer to T
ptr<int> x;
仅可通过 using
指令使用。
并且不要忘记这是C ++ 11的功能。某些编译器尚不支持。
And do not forget that this is a C++11 feature. Some compilers do not support it yet.
这篇关于typedef和C ++ 11类型别名之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!