Topic: templated new/delete
Author: "jam" <farid.mehrabi@gmail.com>
Date: Wed, 22 Nov 2006 15:41:47 CST Raw View
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 ]