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 

templated new/delete

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





PostPosted: Wed Nov 22, 2006 9:41 pm    Post subject: templated new/delete Reply with quote



historically new/delete were introduced prior to templates and forced
them to be too complex to work with .but I think life will be sweeter
if they are treated as templates.current syntax for overloading is:

void * new(size_t must_be_size_t ,other params);
void delete(void* ptr);

I suggest:

template<typename to_be_allocated>
ret_type new(other params) to_be_allocated;
template<typename to_be_freed>
ret_type delete(to_be_freed* ptr);

in which 'ret_type' need not be 'void*' or 'void' but some other user
defined type -especially a smart pointer to the argument type which
increases the security of programs .size_of argument types can
easily be retrived via size_of operator.this syntax allows for global
specialization of new/delete for a 'specific_type' like this:

ret_type new(other params) specific_type {...};

or like this:

template<>
ret_type new (other params) specific_type {...};

or :

template<>
ret_type new<specific_type> (other params) specific_type {...};

this specialization is not inhirited by derrivatives(subclasses) of
course.if there is a need for an inherited version then a static
templated member operator can be introduced :

struct base{
template<typename to_be_allocated>
static ret_type new(other params) to_be_allocated;
template<typename to_be_freed>
static ret_type delete(to_be_freed* ptr);
};

struct derrived:base{};
new(other_args) derrived; //invoke base::new<drrived> new(other_args)
derrived

the benefit of this syntax is that as mentioned formerly ret_type
could be a templated type related to the input argument type.this is
more flexible and secure.

---
[ 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.comeaucomputing.com/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.