While going through the Alexander Stepanov original STL(Standard Template Library) source code, I encountered the following from the memory allocator function file: defalloc.h
template <class T1, class T2>
inline void construct(T1* p, const T2& value) {
new (p) T1(value);
}
I am not able to understand it completely and have the following question/doubts:
- It appears to me that it has something to do with copy constructor of type
T1
? - Why the above function is template on two type
T1
&T2
?. It should have beenT1*
for first and*T1
for second(value). - why
new
has been used in the above logic?. I looked the uses of it and found the following in file vector.h
void push_back(const T& x) { if (finish != end_of_storage) { construct(finish, x); .... .... }
So based on above, finish has already acquired the memory and been passed into it. The other parameter is x which is value of same type T. These are the few concepts which I am able to think/understand.
It appears to me very general yet important function which has been used throughout STL logic. Could somebody can explain it the above concept?