C ++'typedef'vs.'using ... = ...' [英] C++ 'typedef' vs. 'using ... = ...'

查看:224
本文介绍了C ++'typedef'vs.'using ... = ...'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

以下代码编译并运行。我的问题是typedef和使用方法之间的区别是什么重命名模板专业化?

The following code compiles and runs. My question is what is the difference between the "typedef" and "using" method for renaming the template specialization?

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;
}



编辑:



如果没有区别,你会喜欢哪一个? /为什么使用... = ...版本介绍?

If there is no difference, which one would you prefer? / Why was the using ... = ... version introduced?

推荐答案

strong>

They are the same.

引用C ++ 11标准(或草稿具体):

To quote the C++11 standard (or the draft to be specific):


A typedef-name 也可以通过别名声明引入。 使用 关键字
后面的标识符会成为 typedef名称和可选的 attribute-specifier-seq 跟随标识符属于
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 and the optional attribute-specifier-seq following the identifier appertains to that 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.

我认为和typedef说明符相同的语义这样说。

这篇关于C ++'typedef'vs.'using ... = ...'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆