为什么 C++11 中没有模板化的 typedef?
Why there is no templated typedef in C++11?
为什么委员会决定不批准模板化 typedef
和模板化 using
?
template <class T>
using my_vector = std::vector<T>;
合法。
但是
template <class T>
typedef std::vector<T> my_vector<T>;
违法吗?
更新。问题 Why does C++11 not have template typedef? 没有回答。
引用提案 C++ 的模板别名 N1489(强调我的):
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
disavantages 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 bullet is a
placeholder for a type-name. Consequently we do not propose the “typedef
”
syntax.
[4] Herb Sutter, Typedef templates, document no. WG21/1406.
为什么委员会决定不批准模板化 typedef
和模板化 using
?
template <class T>
using my_vector = std::vector<T>;
合法。
但是
template <class T>
typedef std::vector<T> my_vector<T>;
违法吗?
更新。问题 Why does C++11 not have template typedef? 没有回答。
引用提案 C++ 的模板别名 N1489(强调我的):
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 disavantages 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 atypedef
-name. The nameVec
is a name for the familystd::vector<., MyAllocator<.>>
– where the bullet is a placeholder for a type-name. Consequently we do not propose the “typedef
” syntax.[4] Herb Sutter, Typedef templates, document no. WG21/1406.