我知道在C++ 11中我们现在可以using用来写类型别名,比如typedefs:
typedef int MyInt;
Run Code Online (Sandbox Code Playgroud)
从我的理解,相当于:
using MyInt = int;
Run Code Online (Sandbox Code Playgroud)
这种新语法来自于努力表达" template typedef":
template< class T > using MyType = AnotherType< T, MyAllocatorType >;
Run Code Online (Sandbox Code Playgroud)
但是,对于前两个非模板示例,标准中是否还有其他细微差别?例如,typedefs以"弱"方式进行别名.也就是说,它不会创建新类型,而只会创建新名称(这些名称之间隐含的转换).
它是否与using生成新类型相同或是否生成新类型?有什么不同吗?
可能重复:
typedef和在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;
}
Run Code Online (Sandbox Code Playgroud)
如果没有差异,你更喜欢哪一个?/为什么引入使用... = ...版本?