C++ whether a template type is a pointer or not -
possible duplicate:
determine if type pointer in template function
i looking method determine whether template pointer or not @ compiling time. because when t not pointer, program not compile cannot delete normal type variable.
template <typename t> void delete(t &avar) { // if t point delete avar; avar = 0; // if t not point, nothing }
basically, learning create link list(not using stl list) myself. , tried use template in list can take types. when type pointer, want delete (the keyword delete) automatically destructor.
the problem is, writen above, when use int rather pointer of class in list, vc2010 wont compile because cannot delete none pointer variable. looking method, such macro deceide when delete avar should compiled or not according template type
that handy utility, think it's better used assigning null
after using native delete
.
to function considered modifiable pointer type arguments, use
template< typename t > // compiler may substitute t, void delete_ref( t *&arg ); // argument still pointer in case.
to find out whether type pointer, use is_pointer
<type_traits>
in boost, tr1, or c++0x.
Comments
Post a Comment