BitArray/BitFields - Reworking with templates

Philippe Sigaud philippe.sigaud at gmail.com
Mon Jul 30 22:27:42 PDT 2012


On Mon, Jul 30, 2012 at 11:19 PM, Era Scarecrow <rtcvb32 at yahoo.com> wrote:

>> void func(T)(X!T x)
>> {}
>>
>> void main()
>> {
>>     X!bool b;
>>     X!int i;
>>     func(b);
>>     func(i);
>> }
>
>
>  Hmmm i do think that seems right... but if it contains multiple parameters,
> then...?
>
> template X(x1, x2, x3) {
>  struct XT {}
> }
>
>> void func(T)(X!T x) {}
>
> Will this still work?

No. Use a 3-params template or a tuple:

void func(A,B,C)(X!(A,B,C) x) {}

or

void func(Ts...)(X!(Ts) x) {}

or even, more general:

void func(T)(T x) if (is(T t = X!(SomeTypes), SomeTypes...)), because
template constrains are much more powerful and general than fiddling
arguments types.

There was a discussion between Simen Kjaeraas and me on the subject
not long ago. Kenji Hara extended the functionnality of is( , T...)
and did a pull request. I'm not sure it's in 2.060, the request was
done a week ago.


>> void tempFunc(T,U)(T t, U u) if (is(T a == X!(SomeType), SomeType) &&
>> is(U a == X!(SomeType), SomeType)
>>                                                 || is(T == XY) && is(U ==
>> XY))
>> {
>> ...
>> }
>> Is that what you need?
>
>
>  I want to say no... But I haven't actually tested it for my use cases.
> Leaving it unconstrained (without checking) to make it work is a disaster
> waiting to happen: I want T and U to both be of the same template (X in this
> case) but not the same instantiation arguments.

Said like this, it's much more clear for me :)

Note that in the above code, SomeType is a fresh variable in each is()
expression. So, even though I used the same name (shouldn't have done
that), T and U can have different type parameters.

*shameless plug*
Btw, I wrote a tutorial on templates, you can find it here:

https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf

(click on 'view raw', that should download the pdf)

That should shed some light on the matters at hand.


More information about the Digitalmars-d-learn mailing list