Is there any spcecial meaning of “typedef” in C++?
当我阅读stdc++代码时,我对下面的行感到困惑。这是一段来自stl_list.h的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | template <class _Tp, class _Alloc> class _List_base : public _List_alloc_base<_Tp, _Alloc, _Alloc_traits<_Tp, _Alloc>::_S_instanceless> { public: typedef _List_alloc_base<_Tp, _Alloc, _Alloc_traits<_Tp, _Alloc>::_S_instanceless> _Base; /* Is _Base a kind of type?*/ typedef typename _Base::allocator_type allocator_type; _List_base(const allocator_type& __a) : _Base(__a) {/* How can a type be initiated like this?*/ _M_node = _M_get_node(); _M_node->_M_next = _M_node; _M_node->_M_prev = _M_node; } } |
令我困惑的是
它的语法与声明变量的语法完全相同,但它不会为指定类型创建别名:
1 2 | int a; // a is a variable of type int typedef int B; // B is an alias for type int |
(注意,他们经常说
对于静态成员,类型别名的作用域是——在没有其他限定符的情况下可以在类内部访问,而从外部需要限定符(
对于您的具体案例来说,它只是一个简单的引用基类,即
它出现在初始化列表中,因为构造函数正在调用基类的构造函数(即
如果你能以某种方式"扩大"
1 2 3 4 5 6 7 8 9 10 11 | template <class _Tp, class _Alloc> class _List_base : public _Base { public: typedef _List_alloc_base<_Tp, _Alloc, _Alloc_traits<_Tp, _Alloc>::_S_instanceless> _Base; typedef typename _Base::allocator_type allocator_type; _List_base(const allocator_type& __a) : _Base(__a) { |
它用于根据以前存在的类型定义自定义类型。为您可能会说的类型提供别名。
您可以"链接"
1 2 3 | typedef int Id; // Define new type"Id" typedef Id UserId; // Define new type"UserId" based on a previously existing type // This is possible since"Id" was already defined |
然后使用它:
1 | UserId uid = 0; //uid is a variable of type UserId, which is an alias for int |
在您每次使用
您可能会注意到该示例在以下行中也使用了
1 | typedef typename _Base::allocator_type allocator_type; |
注意,该行中可以使用
从这个链接:
The formula to follow is:
typedef [attributes] DataType AliasName; The
typedef keyword is required. The attribute is not.
Thetypedef keyword can be followed by any C++ built-in data type, includingint ,short ,signed ,unsigned ,char ,signed char ,unsigned char ,double ,long , orlong double . The data type can also be an existing class that either is provided by one of the libraries that ship with the C++ compiler. For example, it can be the string class. The data type can also be a pointer to a known type.
尽管这是一个定义,你会遇到很多次为这个关键字,它可能更容易认为它是