Inferring function argument types from other argument types

Vijay Nayar madric at gmail.com
Tue Nov 13 09:08:31 PST 2012


Ok, I get it.

My understanding is that you have basically two options.
   * If you want the function to be called with any implicitly
     castable type, then you must cast it in the function.
   * If you only want the function to accept the one type,
     then use is(T == FooT.T1) and you must cast the input
     the function.

However, all this feels like an over complication and round-about 
way of
doing things.  If you have exact types that must be specified in 
the
struct, you probably don't want an external template at all.  
Simply place
the function in the struct.

struct Foot(_T1, _T2) {
   ...
   _T1 func(_T1 x) {
     return func2(x);
   }
}

  - Vijay

On Tuesday, 13 November 2012 at 16:39:55 UTC, Joseph Rushton 
Wakeling wrote:
> On 11/13/2012 05:05 PM, Vijay Nayar wrote:
>> I believe this question was asked before, but here is the 
>> solution again.
>
> The actual reality of what I'm trying to do is slightly more 
> complex: it's more like
>
>     struct Foo(_T1, _T2)
>     {
>         alias _T1 T1;
>         alias _T2 T2;
>         // etc.
>     }
>
>     FooT.T1 func(FooT, T)(FooT foo, T x)
>     {
>         return func2(x);
>     }
>
> ... where func2() is also a templated function, and it's 
> important that it take a type of FooT.T1 and not T.
>
> So, I can't see what the solution is apart from casting x to 
> FooT.T or explicitly indicating FooT.T1 as a template parameter 
> for func2.




More information about the Digitalmars-d-learn mailing list