Use template functions within mixin

Patrick Schluter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 6 08:12:38 PDT 2017


On Tuesday, 6 June 2017 at 15:00:50 UTC, Timoses wrote:
> Hey there,
>
> I'm wondering how I can use a template function within my mixin:
>
> ```
>                 ubyte[] value = x[33, 3a,3f, d4];
>                 foreach (type; TypeTuple!("int", "unsigned 
> int", "byte"))
>                 {
>                     mixin(`if (value.length == type.sizeof)
>                             {
>                                 ubyte[type.sizeof] raw = 
> value[0..$];
>                                 auto fValue = 
> raw.littleEndianToNative!(type);
>                                 displayinfo(fValue);
>                             }
>                             break;
>                         `);
>                 }
> ```
>
>
> Error: template std.bitmanip.littleEndianToNative cannot deduce 
> function from argument types !("int")(ubyte[8]), candidates are:
> [..]\src\phobos\std\bitmanip.d(2560,3):        
> std.bitmanip.littleEndianToNative(T, uint n)(ubyte[n] val) if 
> (canSwapEndianness!T && n == T.sizeof)
>
> ```
> `raw.littleEndianToNative!` ~ type ~ `;`
> ```
>
Did you also put the ` ~ type ~ ` on the 2 other cases where you 
use the variable type?

      mixin(`if (value.length == ` ~ type ~ `.sizeof)
                {
                   ubyte[` ~ type ~ `.sizeof] raw = value[0..$];
                   auto fValue = raw.littleEndianToNative!(` ~ 
type ~ `);
                   displayinfo(fValue);
                 }
                  break;
             `);



More information about the Digitalmars-d-learn mailing list