"using" 创建类型别名的论据是否成立?
Is the argument for "using" to create a type-alias well-established?
C++11 中有一个用于创建类型别名的新语法,using
。它可以使用,其中使用 typedef
,并且可以模板化。支持 using
的 explanation 是这样的:
It has been suggested to (re)use the keyword typedef — as done in the
paper [4] — to introduce template aliases:
template<class T>
typedef std::vector<T,MyAllocator<T>> Vec;
That notation has the advantage of using a keyword already known to introduce a type alias. However, it also displays several disadvantages among which the confusion of using a keyword known to introduce an alias for a type-name in a context where the alias does not designate a type, but a template; Vec
is not an alias for a type, and should not be taken for a typedef-name. The name Vec
is a name for the family std::vector<*,MyAllocator<*>>
- where the asterisk is a placeholder for a type-name.
Consequently, we do not propose the "typedef" syntax.
template<class T>
using Vec = std::vector<T,MyAllocator<T>>;
can be read/interpreted as: from now on, I'll be using Vect<T>
as a synonym for std::vector<T,MyAllocator<T>>
. With that reading, the new syntax for aliasing seems reasonably logical.
但是,我不明白。我们为 classes、函数使用模板,我们没有为它们单独设置关键字。那为什么我们有一个单独的关键字 typedef
?
即:
class Foo {
};
template <typename>
class Bar {
};
我们对 Foo
和 Bar
使用 class
,Foo
是实际的 class,但 Bar
是模板, "collection" 个 classes.
有人可以解释一下吗?
除了(IMO)更大的可读性论点之外,别名模板不仅仅是别名(就像 typedef 一样,您无法以任何方式将 typedef 与其别名类型区分开来):它们真正代表了一个类型家族,例如,你可以写:
template<class T>
using foo = ...;
template<template<typename> class TT> void bar();
bar<foo>();
所以 foo 不仅仅是一个别名,它是一个不同的野兽;使用 typedef 可能会让人感到困惑......我认为这就是 "Vec is not an alias for a type, and should not be taken for a typedef-name".
的意思
C++11 中有一个用于创建类型别名的新语法,using
。它可以使用,其中使用 typedef
,并且可以模板化。支持 using
的 explanation 是这样的:
It has been suggested to (re)use the keyword typedef — as done in the paper [4] — to introduce template aliases:
template<class T> typedef std::vector<T,MyAllocator<T>> Vec;
That notation has the advantage of using a keyword already known to introduce a type alias. However, it also displays several disadvantages among which the confusion of using a keyword known to introduce an alias for a type-name in a context where the alias does not designate a type, but a template;
Vec
is not an alias for a type, and should not be taken for a typedef-name. The nameVec
is a name for the familystd::vector<*,MyAllocator<*>>
- where the asterisk is a placeholder for a type-name. Consequently, we do not propose the "typedef" syntax.template<class T> using Vec = std::vector<T,MyAllocator<T>>;
can be read/interpreted as: from now on, I'll be using
Vect<T>
as a synonym forstd::vector<T,MyAllocator<T>>
. With that reading, the new syntax for aliasing seems reasonably logical.
但是,我不明白。我们为 classes、函数使用模板,我们没有为它们单独设置关键字。那为什么我们有一个单独的关键字 typedef
?
即:
class Foo {
};
template <typename>
class Bar {
};
我们对 Foo
和 Bar
使用 class
,Foo
是实际的 class,但 Bar
是模板, "collection" 个 classes.
有人可以解释一下吗?
除了(IMO)更大的可读性论点之外,别名模板不仅仅是别名(就像 typedef 一样,您无法以任何方式将 typedef 与其别名类型区分开来):它们真正代表了一个类型家族,例如,你可以写:
template<class T>
using foo = ...;
template<template<typename> class TT> void bar();
bar<foo>();
所以 foo 不仅仅是一个别名,它是一个不同的野兽;使用 typedef 可能会让人感到困惑......我认为这就是 "Vec is not an alias for a type, and should not be taken for a typedef-name".
的意思