Inferring function argument types from other argument types

Vijay Nayar madric at gmail.com
Tue Nov 13 08:05:28 PST 2012


I believe this question was asked before, but here is the 
solution again.

struct Foo(_T1, _T2)
{
   alias _T1 T1;
   alias _T2 T2;
   T1 a;
   T2 b;
}

FooT.T1 func(FooT, T)(FooT foo, T x)
   if (is(FooT.T1) && is(T : FooT.T1))
{
   return x * foo.a;
}

void main() {
   auto foo = Foo!(size_t, string)(8, "bobcat");
   int value = 3;
   assert(foo.func(value) == 24);
   assert(is(typeof(foo.func(value)) == size_t));
}

  - Vijay

On Monday, 12 November 2012 at 12:42:31 UTC, Joseph Rushton 
Wakeling wrote:
> Suppose that I've got a struct which internally defines a 
> number of types:
>
>     struct Foo(_T1, _T2)
>     {
>         alias _T1 T1;
>         alias _T2 T2;
>         T1 a;
>         T2 b;
>     }
>
> ... and now I want to define a function which takes as input an 
> instance of one of these structs, and a variable of type T1.
>
> I tried the following:
>
>     T func(FooT, T = FooT.T1)(FooT foo, T x)
>     {
>         return x * foo.a;
>     }
>
> but found that the type of whatever I was passing would 
> override the default: e.g. if I called func(fooInstance, 1) 
> then the second argument would be interpreted as an int even 
> though Foo.T1 is size_t.
>
> I also tried,
>
>     T func(FooT, T : FooT.T1)(FooT foo, T x)
>     {
>         return x * foo.a;
>     }
>
> but this generates a different error: "no property 'T1' for 
> type 'FooT'".  I tried replacing FooT with alias FooT to no 
> avail.
>
> Obviously I could get round the problems of the first example 
> with something based around CommonType etc. but I'm just 
> wondering if it's possible to make an argument dependent on 
> another template parameter in the way I'm looking for here.




More information about the Digitalmars-d-learn mailing list