"知道在C ++ 11中,们现在可以使用using写入类型别名,如typedefs:
typedef int MyInt;
据所知,相当于:
using MyInt = int;
这种新的语法来自于努力去表达“ template typedef”:
template< class T > using MyType = AnotherType< T, MyAllocatorType >;
但是,在前两个非模板的例子中,标准还有其他的细微差别吗?例如,typedefs以“弱”方式进行别名。也就是说,它不会创建新的类型,而只是一个新的名称(这些名称之间的转换是隐含的)。
它是一样的using还是它产生一个新的类型?有什么区别?"
在使用中的模板中使用时的语法有优势。如果您需要抽象类型,而且还需要保留模板参数以后可以指定。你应该写这样的东西。
template <typename T> struct whatever {};
template <typename T> struct rebind
{
typedef whatever<T> type; // to make it possible to substitue the whatever in future.
};
rebind<int>::type variable;
template <typename U> struct bar { typename rebind<U>::type _var_member; }
但是使用语法简化了这个用例。
template <typename T> using my_type = whatever<T>;
my_type<int> variable;
template <typename U> struct baz { my_type<U> _var_member; }