【问题标题】:C++ 'typedef' vs. 'using ... = ...' [duplicate]C ++'typedef'与'使用... = ...' [重复]
【发布时间】:2012-06-28 18:35:21
【问题描述】:

可能重复:
What are the differences between typedef and using in C++11?

以下代码编译并运行。我的问题是重命名模板专业化的“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;
}

编辑:

如果没有区别,你更喜欢哪一个? / 为什么要引入 using ... = ... 版本?

【问题讨论】:

标签: c++ c++11 typedef using-directives


【解决方案1】:

它们是一样的。

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

typedef-name 也可以由 alias-declaration 引入。 using 关键字后面的标识符 成为一个 typedef-name 并且标识符后面的可选 attribute-specifier-seq 属于该标识符 类型定义名称。它具有与 typedef 说明符引入的语义相同的语义。特别是,它 没有定义一个新的类型并且它不会出现在type-id中。

我认为 “与 typedef 说明符相同的语义” 说明了一切。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-29
    • 2015-08-10
    • 2020-03-21
    • 2022-10-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 2016-01-16
    相关资源
    最近更新 更多