www.digitalmars.com         C & C++   DMDScript  

D - Detecting Pointer Types

reply resistor mac.com writes:
I'm writing the destruction for a templated class.  It has a member variable of
type T (the template's type).  I want to somehow detect if this is a pointer
type, and set it to null if it is.  Is there some way to detect if it's a point
type, or will I have to make a specialized version of the class to handle it?

Owen
Feb 26 2004
parent reply Sean Kelly <sean ffwd.cx> writes:
resistor mac.com wrote:
 I'm writing the destruction for a templated class.  It has a member variable of
 type T (the template's type).  I want to somehow detect if this is a pointer
 type, and set it to null if it is.  Is there some way to detect if it's a point
 type, or will I have to make a specialized version of the class to handle it?

template IsPointer( T ) { static const bit IsPointer = false; } tempalte IsPointer( T : T* ) { static const bit IsPointer = true; } if( IsPointer!( T ) ) t = NULL; You stull might get a compiler error trying to set T to NULL depending on what T is. You may want to do something like this instead: template SetNULL( T ) { void SetNULL( T val ) {} } template SetNULL( T : T* ) { void SetNULL( T val ) { val = NULL; } } SetNULL!(T)( t );
Feb 26 2004
parent reply resistor mac.com writes:
That seems rather...hackish.  I'm sure it would work, but it seems rather
strange to have to do that.  Has nobody else ever needed to write a ~this() for
a templated class?

Owen

In article <c1m53v$2gf5$1 digitaldaemon.com>, Sean Kelly says...
resistor mac.com wrote:
 I'm writing the destruction for a templated class.  It has a member variable of
 type T (the template's type).  I want to somehow detect if this is a pointer
 type, and set it to null if it is.  Is there some way to detect if it's a point
 type, or will I have to make a specialized version of the class to handle it?

template IsPointer( T ) { static const bit IsPointer = false; } tempalte IsPointer( T : T* ) { static const bit IsPointer = true; } if( IsPointer!( T ) ) t = NULL; You stull might get a compiler error trying to set T to NULL depending on what T is. You may want to do something like this instead: template SetNULL( T ) { void SetNULL( T val ) {} } template SetNULL( T : T* ) { void SetNULL( T val ) { val = NULL; } } SetNULL!(T)( t );

Feb 26 2004
next sibling parent Sam McCall <tunah.d tunah.net> writes:
resistor mac.com wrote:

 That seems rather...hackish.  I'm sure it would work, but it seems rather
 strange to have to do that.  Has nobody else ever needed to write a ~this() for
 a templated class?

If it's for a destructor, would foo=T.init work? Sam
Feb 26 2004
prev sibling next sibling parent reply John Reimer <jjreimer telus.net> writes:
On Fri, 27 Feb 2004 04:04:03 +0000, resisto wrote:

 That seems rather...hackish.  I'm sure it would work, but it seems rather
 strange to have to do that.  Has nobody else ever needed to write a ~this() for
 a templated class?
 
 Owen
 
 In article <c1m53v$2gf5$1 digitaldaemon.com>, Sean Kelly says...


I thought it to be rather novel and creative. It's an interesting example of the flexibility of templates.
Feb 26 2004
next sibling parent reply resistor mac.com writes:
Sorry if that sounded insulting.  I didn't mean to be.  I just meant that it
seemed like something that shouldn't require such arcane antics to achieve.  I
certainly am impressed with his solution.  It was more a comment on the need to
do that at all than on his solution.

Sorry if I offended anyone.

Owen

In article <pan.2004.02.27.06.39.55.658007 telus.net>, John Reimer says...
On Fri, 27 Feb 2004 04:04:03 +0000, resisto wrote:

 That seems rather...hackish.  I'm sure it would work, but it seems rather
 strange to have to do that.  Has nobody else ever needed to write a ~this() for
 a templated class?
 
 Owen
 
 In article <c1m53v$2gf5$1 digitaldaemon.com>, Sean Kelly says...


I thought it to be rather novel and creative. It's an interesting example of the flexibility of templates.

Feb 27 2004
parent John Reimer <John_member pathlink.com> writes:
In article <c1njd1$1r5t$1 digitaldaemon.com>, resistor mac.com says...
Sorry if that sounded insulting.  I didn't mean to be.  I just meant that it
seemed like something that shouldn't require such arcane antics to achieve.  I
certainly am impressed with his solution.  It was more a comment on the need to
do that at all than on his solution.

Sorry if I offended anyone.

Owen

No, no, no. Don't worry about it. You were not insulting at all. You are perfectly entitled to your opinion. For your purposes, the solution may have been more convoluted than desired. I was just balancing out the perspective: if not a practical solution, it certainly was original :-). Later, John
Feb 27 2004
prev sibling parent reply Matthias Becker <Matthias_member pathlink.com> writes:
 That seems rather...hackish.  I'm sure it would work, but it seems rather
 strange to have to do that.  Has nobody else ever needed to write a ~this() for
 a templated class?
 
 Owen
 
 In article <c1m53v$2gf5$1 digitaldaemon.com>, Sean Kelly says...


I thought it to be rather novel and creative. It's an interesting example of the flexibility of templates.

This is very common technique in C++. Actualy this was the simplest kind of template-meta-programming.
Mar 01 2004
parent reply John Reimer <jjreimer telus.net> writes:
On Mon, 01 Mar 2004 14:48:22 +0000, Matthias Becker wrote:

 That seems rather...hackish.  I'm sure it would work, but it seems rather
 strange to have to do that.  Has nobody else ever needed to write a ~this() for
 a templated class?
 
 Owen
 
 In article <c1m53v$2gf5$1 digitaldaemon.com>, Sean Kelly says...


I thought it to be rather novel and creative. It's an interesting example of the flexibility of templates.

This is very common technique in C++. Actualy this was the simplest kind of template-meta-programming.

Oh dear, I guess I better study up on my template-meta-programming then :-).
Mar 01 2004
parent reply Sean Kelly <sean ffwd.cx> writes:
John Reimer wrote:
 
 Oh dear, I guess I better study up on my template-meta-programming then
 :-).

If you're so inclined, there's a book called "Generative Programming" that covers the topic pretty thoroughly without focusing too heavily on C++. Sean
Mar 02 2004
parent John Reimer <jjreimer telus.net> writes:
Sean Kelly wrote:
 John Reimer wrote:
 
 Oh dear, I guess I better study up on my template-meta-programming then
 :-).

If you're so inclined, there's a book called "Generative Programming" that covers the topic pretty thoroughly without focusing too heavily on C++. Sean

Thanks for the tip, Sean. I'll look into it.
Mar 02 2004
prev sibling parent Sean Kelly <sean ffwd.cx> writes:
resistor mac.com wrote:

 That seems rather...hackish.  I'm sure it would work, but it seems rather
 strange to have to do that.  Has nobody else ever needed to write a ~this() for
 a templated class?

Not sure I understand. If your pointer is a class member then it will disappear when the dtor exits and the gc will wipe out any orphaned data. Sean
Feb 27 2004