May 20, 2005
"Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:d6lf5g$1iv7$1@digitaldaemon.com...
> Are there some motivating examples for "static if" and iftype? The doc example illustrate the behavior but don't do anything useful so I'm not
sure
> when to use these constructs vs template specialization.

www.digitalmars.com/d/cpptod.html, look at the last example


May 20, 2005
imagine that somewhere I have a constant WINVER and
enum winver{
    WIN95,
    WIN98,
    WINME,
    WIN2K,
    WINXP,
    WIN2K3,
    WINXP64
}

so I can do something like this (statically)

static if( WINVER <=  WINME) // mixin if
    .... 16bit GDI ops...
else  if ( WINVER >=  WINXP64)
    .... 64bit GDI ops...
else
    .... 32bit GDI ops...

In fact if there are just few choices in enum winver I will probably do this by using mixins or probably version.

mixin when (iftype) is a bit more interesting as it allows you to do instantiation more deterministic and flexible:

mixin when( coordinate : long, color : long  ) ....
  else when( coordinate : int, color : long  ) ....
  else when( coordinate : long  ) ....
  else when( coordinate : int  ) ....
  else static assert(0, "unsuppported target configuration");

-----------------------------------------------
But of course we can live without this ...
It is just a matter of comfort I would say.

Andrew.



"Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:d6lf5g$1iv7$1@digitaldaemon.com...
>
> "Walter" <newshound@digitalmars.com> wrote in message news:d6imvd$2c53$1@digitaldaemon.com...
>> I'm not too sure about iftype. Let's see how it goes. Consider it 'experimental' for now.
>>
>> http://www.digitalmars.com/d/changelog.html
>
> Are there some motivating examples for "static if" and iftype? The doc example illustrate the behavior but don't do anything useful so I'm not sure when to use these constructs vs template specialization.
> 


May 20, 2005
Andrew Fedoniouk wrote:
> imagine that somewhere I have a constant WINVER and
> enum winver{
>     WIN95,
>     WIN98,
>     WINME,
>     WIN2K,
>     WINXP,
>     WIN2K3,
>     WINXP64
> }
> 

Hey... that's not hard to imagine!  It looks very familiar. ;-)

-JJR
May 21, 2005
"Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d6lj2t$1lqg$1@digitaldaemon.com...
>
> imagine that somewhere I have a constant WINVER and
> enum winver{
>    WIN95,
>    WIN98,
>    WINME,
>    WIN2K,
>    WINXP,
>    WIN2K3,
>    WINXP64
> }
>
> so I can do something like this (statically)
>
> static if( WINVER <=  WINME) // mixin if
>    .... 16bit GDI ops...
> else  if ( WINVER >=  WINXP64)
>    .... 64bit GDI ops...
> else
>    .... 32bit GDI ops...
>
> In fact if there are just few choices in enum winver I will probably do
> this
> by using mixins or probably version.

makes sense.

> mixin when (iftype) is a bit more interesting as it allows you to do instantiation more deterministic and flexible:
>
> mixin when( coordinate : long, color : long  ) ....
>  else when( coordinate : int, color : long  ) ....
>  else when( coordinate : long  ) ....
>  else when( coordinate : int  ) ....
>  else static assert(0, "unsuppported target configuration");

Iftype is actually pretty fun. I've started using iftype in MinTL to support custom memory allocators. For example a couple of simple used in Deque are

struct Deque(Value, Alloc = GCAllocator) {
  void clear() {
    iftype (Alloc : GCAllocator) {
    } else {
      foreach (Block p; data) {
        Alloc.freeData(p);
      }
      Alloc.free(data.ptr);
    }
    *this = Deque.init;
  }
  Block newBlock() {
    iftype (Alloc : GCAllocator) {
      return new Value[BlockSize];
    } else {
      Value* p = cast(Value*)Alloc.mallocData(BlockSize * Value.sizeof);
      Value[] q = p[0..BlockSize];
      q[] = Value.init;
      return p;
    }
  ...
}

So this way a user can declare
  Deque!(int) x;
to get a standard entirely-GC allocator or use a predefined allocator like
  Deque!(int,Malloc) y;
that is aliased to use std.c.stdlib.malloc and friends. Another one of the
predefined allocators is GCMalloc that allocates data from the GC since it
may contain root and non-data from malloc. Of course using malloc one has to
be careful to not leak but that's expected.

Anyhow, maybe I'll also cook up an allocator to work around the +1 issue that makes GC allocations so inefficient. I haven't tried that yet.

> -----------------------------------------------
> But of course we can live without this ...
> It is just a matter of comfort I would say.
>
> Andrew.
>
>
>
> "Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:d6lf5g$1iv7$1@digitaldaemon.com...
>>
>> "Walter" <newshound@digitalmars.com> wrote in message news:d6imvd$2c53$1@digitaldaemon.com...
>>> I'm not too sure about iftype. Let's see how it goes. Consider it 'experimental' for now.
>>>
>>> http://www.digitalmars.com/d/changelog.html
>>
>> Are there some motivating examples for "static if" and iftype? The doc example illustrate the behavior but don't do anything useful so I'm not sure when to use these constructs vs template specialization.


1 2 3 4
Next ›   Last »