Teacher Hou - STL Source Code Analysis - Doubt in the book

zhaozj2021-02-11  207

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 spatial configurator, when the system calls construct (...), its internal delegate _construct (...) is executed, at this time the template function _contruct T1 is Pointer (ie int *), when executing New (p) T1 (Value); when the statement, it becomes new int * (value). Can this be compiled? Even if it is, it is not what we want. Do you think? To this end, I saw the STL's implementation source code, and I found that I was different from Teacher Hou. The source code is as follows:

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 struct rebind {

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

转载请注明原文地址:https://www.9cbs.com/read-5581.html

New Post(0)