C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why allocator.construct() can only copy construct an object?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Dhruv
Guest





PostPosted: Wed Oct 29, 2003 10:44 am    Post subject: Why allocator.construct() can only copy construct an object? Reply with quote



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 ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.