C++ 'typedef' vs. '使用 ... = ...'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11224336/
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
C++ 'typedef' vs. 'using ... = ...'
提问by Simon
Possible Duplicate:
What are the differences between typedef and using in C++11?
The following code compiles and runs. My question is what is the difference between the "typedef" and "using" method for renaming the template specialization?
以下代码编译并运行。我的问题是重命名模板特化的“typedef”和“using”方法有什么区别?
template<typename T>
struct myTempl{
T val;
};
int main (int, char const *[])
{
using templ_i = myTempl<int>;
templ_i i;
i.val=4;
typedef myTempl<float> templ_f;
templ_f f;
f.val=5.3;
return 0;
}
Edit:
编辑:
If there is no difference, which one would you prefer? / Why was the using ... = ... version introduced?
如果没有区别,你更喜欢哪一个?/ 为什么引入 using ... = ... 版本?
回答by daramarak
They are the same.
他们是一样的。
To quote the C++11 standard (or the draft to be specific):
引用 C++11 标准(或具体的草案):
A typedef-namecan also be introduced by an alias-declaration. The identifier following the usingkeyword becomes a typedef-nameand the optional attribute-specifier-seqfollowing the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedefspecifier. In particular, it does not define a new type and it shall not appear in the type-id.
一个typedef的名称,也可以通过引入的别名声明。using关键字后面的标识符成为typedef-name,标识符后面的可选属性说明符-seq 属于该 typedef-name。它与由typedef说明符引入的语义相同。特别是,它没有定义新类型,也不会出现在type-id 中。
I think the "the same semantics as the typedef specifier"say it all.
我认为“与 typedef 说明符相同的语义”说明了一切。