Thread overview
How to compile this template with short type?
Sep 17, 2013
mrd
Sep 17, 2013
Namespace
Sep 17, 2013
mrd
September 17, 2013
[code]
Unsigned!T encodeZigZag( T )( inout T v ) pure
if( isSigned!( T ) )
{
     return v > 0
         ?
             v * 2
         :
             -v * 2 - 1;
}
unittest
{
     assert( encodeZigZag!long( 2147483647 ) == 4294967294 );
     assert( encodeZigZag!long( -2147483648 ) == 4294967295 );

     assert( encodeZigZag!short( 20 ) == 40 );
     assert( encodeZigZag!short( -20 ) == 39 );
}
[/code]

Error: cannot implicitly convert expression (cast(int)v > 0 ?
cast(int)v * 2 : cast(int)-v * 2 - 1) of type int to ushort

int?!
September 17, 2013
On Tuesday, 17 September 2013 at 07:58:40 UTC, mrd wrote:
> [code]
> Unsigned!T encodeZigZag( T )( inout T v ) pure
> if( isSigned!( T ) )
> {
>      return v > 0
>          ?
>              v * 2
>          :
>              -v * 2 - 1;
> }
> unittest
> {
>      assert( encodeZigZag!long( 2147483647 ) == 4294967294 );
>      assert( encodeZigZag!long( -2147483648 ) == 4294967295 );
>
>      assert( encodeZigZag!short( 20 ) == 40 );
>      assert( encodeZigZag!short( -20 ) == 39 );
> }
> [/code]
>
> Error: cannot implicitly convert expression (cast(int)v > 0 ?
> cast(int)v * 2 : cast(int)-v * 2 - 1) of type int to ushort
>
> int?!

----
Unsigned!T encodeZigZag( T )( inout T v ) pure
if( isSigned!( T ) )
{
     return cast(Unsigned!T)(v > 0
         ?
             v * 2
         :
             -v * 2 - 1);
}
----
September 17, 2013
ahhh, many thanks!

I need more sleep