How to compile this template with short type?

Namespace rswhite4 at googlemail.com
Tue Sep 17 01:10:11 PDT 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);
}
----


More information about the Digitalmars-d-learn mailing list