Jump to page: 1 2
Thread overview
Detecting Pointer Types
Feb 26, 2004
resistor
Feb 27, 2004
Sean Kelly
Feb 27, 2004
resistor
Feb 27, 2004
Sam McCall
Feb 27, 2004
John Reimer
Feb 27, 2004
resistor
Feb 27, 2004
John Reimer
Mar 01, 2004
Matthias Becker
Mar 02, 2004
John Reimer
Mar 02, 2004
Sean Kelly
Mar 02, 2004
John Reimer
Feb 27, 2004
Sean Kelly
February 26, 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


February 27, 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 );

February 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...
>
>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 );
>


February 27, 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
February 27, 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.
February 27, 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.


February 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


February 27, 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

March 01, 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.


March 02, 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 :-).


« First   ‹ Prev
1 2