D - Detecting Pointer Types
- resistor mac.com Feb 26 2004
- Sean Kelly <sean ffwd.cx> Feb 26 2004
- resistor mac.com Feb 26 2004
- Sam McCall <tunah.d tunah.net> Feb 26 2004
- John Reimer <jjreimer telus.net> Feb 26 2004
- resistor mac.com Feb 27 2004
- John Reimer <John_member pathlink.com> Feb 27 2004
- Matthias Becker <Matthias_member pathlink.com> Mar 01 2004
- John Reimer <jjreimer telus.net> Mar 01 2004
- Sean Kelly <sean ffwd.cx> Mar 02 2004
- John Reimer <jjreimer telus.net> Mar 02 2004
- Sean Kelly <sean ffwd.cx> Feb 27 2004
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
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
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
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
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
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
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
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
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
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
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
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









Sam McCall <tunah.d tunah.net> 