BitArray/BitFields - Reworking with templates

Philippe Sigaud philippe.sigaud at gmail.com
Mon Jul 30 13:48:16 PDT 2012


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

>  A question regarding templates. A template with different parameters is
> completely incompatible correct?

Correct. They have no reason, in general, too even generate the same code:

template Chameleon(T)
{
    static if (is(T == struct))
        enum Chameleon = "I'm a string!"; // Chamelon!(SomeStruct) is
a string value
    else static if (is(T == class))
        void Chameleon(int i) { return i+1;} // Chameleon!(SomeClass)
is a function
    else
        alias T[int] Chameleon; // Here Chameleon is a type
}

The same for you struct S(T) { ... }. Inside the braces, anything can
happen (mwwaahhha hhaa!), depending on T.

> So...
>
> struct X(T) {
> }
>
> alias X!bool XB;
> alias X!int XI;
>
> void func(XB xb) {
>
> }
>
> func(XB()); //works
> func(XI()); //should fail to compile

Yes.

>  Now if all that is correct, say I want to make two functions that both use
> X, but are not compatible, but template functions will allow it. So...

I'm not sure I understand what you're trying to do. Do you mean you
want a function that accept X!(T)s for any T, and not any other type?

struct X(T) {}

void func(T)(X!T x)
{}

void main()
{
    X!bool b;
    X!int i;
    func(b);
    func(i);
}

> void tempFunc(T, U)(T t, U u)
> if(
>   //What do i enter to check they are both template X or struct XY? As
> signatures will be different...
> )
> body{
>   //now acts as a middle-man to be compatible for said operation
> }

Here you want a constraint that checks that U and T are both
X!(SomeType) or an XY?

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?


More information about the Digitalmars-d-learn mailing list