Teacher Hou - STL Source Code Analysis - Two Mistles in the Book
Foreword
I am watching Houjie's STL source code profile "Chapter 2 Design a Space Configurator JJ :: Allocator (page 45) See the pseudo code, I think it is wrong, the code is as follows:
Name Space JJ China Code
......
Template
Inline void _Construct (T1 * P, Const T2 & Value)
{
NEW (P) T1 (Value);
}
......
Template
Class Allocator
{
......
Typedef t * pointer;
......
Void Construct (Pointer P, Const T & Value)
{
_Construct (p, value);
}
......
}
Let us now assume that using the Allocator
SGI's STL source code
Template
Struct __allocator {
_ALLOC __UNDERLYING_ALLOC;
TYPEDEF SIZE_T SIZE_TYPE;
TYPEDEF PTRDIFF_T DIFCERENCE_TYPE;
Typedef _tp * Pointer;
Typedef const_point;
Typedef_tp & reference;
Typedef const_tp & const_reference;
Typedef _tp value_type;
Template
TYPEDEF __ALLOCATOR <_tp1, _alloc> ta;
}
__allocator () __STL_NothRow {}
__allocator (const __allocator& __a) __stl_nothrow
: __underlying_alloc (__ a .__ underlying_alloc) {}
Template
__allocator (const __allocator <_tp1, _alloc> & __a) __stl_nothrow
: __underlying_alloc (__ a .__ underlying_alloc) {}
~ __allocator () __STL_NothRow {}
Pointer Address (Reference __x) const {return & __ x;}
Const_Pointer Address (const_reference __x) const {return & __ x;}
// __n is permitted to be 0.
_Tp * allocate (size_type __n, const void * = 0) {
Return __n! = 0
? static_cast <_tp *> (__ underlying_alloc.allocate (__ n * sizeof (_tp)): 0;
}
// __p is not permitted to be a null pointer.
Void Deallocate (Pointer __P, SIZE_TYPE __N)
{__underlying_alloc.deallocate (__ p, __n * sizeof (_tp));
SIZE_TYPE MAX_SIZE () const __stl_nothrow
{RETURN SIZE_T (-1) / sizeof (_tp);
Void construct (Pointer __P, const _tp & __val) {new (__ p) _tp (__ var);
Void Destroy (Pointer __P) {__P-> ~ _tp ();
}
Pay attention to the black part, the source code is __tp (already value_type), so Teacher Teacher's _construct function should be changed as follows:
Template
Inline void _Construct (T1 P, Const T2 & Value)
{
NEW (P) T2 (Value);
}
First, I declare that I am very very worshiped by Teacher, and Teacher Hou's book is really very good, so she looks carefully, but this does not mean that I have said something, Hou teacher must be wrong. I just wrote my personal opinion. Please criticize advice and make progress together! If you can get the teacher's pointing, it is better to be better. ^ _ ^.
In short, please have a lot of criticism to believe to ccplusplus@21cn.com.
Acknowledgments!
Yuan Kai
2001-11-13