| Thread overview |
|---|
September 17, 2013 How to compile this template with short type? | ||||
|---|---|---|---|---|
| ||||
[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 Re: How to compile this template with short type? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to mrd | 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);
}
----
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply