Topic: Why allocator.construct() can only copy construct objects?
Author: dhruvbird@gmx.net ("Dhruv")
Date: Thu, 23 Oct 2003 20:24:52 +0000 (UTC) Raw View
I wanted to know if there is any reason to as to why allocator.construct()
can only copy construct an object, and not construct an object form any
other type as long as that type is a parameter in the object's constructor
parameter list. Like:
Currently, construct is defined as:
template <class T>
void some_allocator<T>::construct (T *ptr, const T& data);
So, why not define it as:
template <class T, class U>
void some_allocator<T>::construct (T *ptr, const U& data);
This would be very helpful in containers like linked lists, and red-balck
trees, which are used by STL's map and multimpa containers. Basically,
node based containers will have better code this way.
Currently, if I want to construct an object in say, a linked list node, I
have to manually construct the object there, by giving the address of that
variable inside the node. Something like this:
tempate <class T>
struct Node { //some member variables;
T data;
Node (const T& _data): data(_data) { }
};
Node *node;
Allocator<T> a;
T data;
To construct data at node.data, we have to do this:
//Allocate memory, and assign it to node.
a.construct (&(node->data), data);
Instead, why not do this:
a.construct (node, data);
Because the data is a parameter that is accepted by the Node's construtor,
we are guaranted that the node object will be in a valid state.
Regards,
-Dhruv.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]