Template bug?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Jan 30 07:07:04 PST 2007


Jarrett Billingsley wrote:
> "Artyom Shalkhakov" <artyom.sh at gmail.ru> wrote in message 
> news:epmlh5$2crr$1 at digitaldaemon.com...
> 
>> Okay, I've tried to use pointer to class because I intend to use this 
>> template for both structures and classes. How do I do that? Do I have to 
>> write template specialization?
> 
> Yep.  You can do something like:
> 
> struct temp_t( type : Object ) {
>     void setOwner( type newOwner ) {
>         owner = newOwner;
>     }
> 
>     type getOwner() {
>         return owner;
>     }
> 
>     protected {
>        temp_t *  head;
>        temp_t *  next;
>        temp_t *  prev;
>        type      owner;
>     }
> }
> 
> struct temp_t( type ) {
>     void setOwner( type *newOwner ) {
>         owner = newOwner;
>     }
> 
>     type *getOwner() {
>         return owner;
>     }
> 
>     protected {
>        temp_t *  head;
>        temp_t *  next;
>        temp_t *  prev;
>        type *      owner;
>     }
> }

static if + is-expressions (and creative 'alias' usage) clean that up 
pretty well:
-----
struct temp_t( type ) {
     static if(is(type == class))
         alias type RefType;
     else
         alias type* RefType;

     void setOwner( RefType newOwner ) {
         owner = newOwner;
     }

     RefType getOwner() {
         return owner;
     }

     protected {
        temp_t *  head;
        temp_t *  next;
        temp_t *  prev;
        RefType   owner;
     }
}
-----

Code duplication is ugly.


More information about the Digitalmars-d-learn mailing list