On 10.12.2016 09:09, Is it possible to store different generic types? wrote:
I'm pretty sure the following code should work, but it doesn't.

```
bool intersect(ptrdiff_t targetX, ptrdiff_t targetY, size_t targetWidth,
size_t targetHeight, ptrdiff_t x, ptrdiff_t y, size_t width, size_t height)
{
 return targetX < x + width &&
       x < targetX + targetWidth &&
       targetY < y + height &&
       y < targetY + targetHeight;
}

void main() {
    import std.stdio;
    writeln(intersect(0,0,800,600,     0,150,148,148));
    writeln(intersect(0,0,800,600,     -10,150,148,148));
}
```

It outputs:
```
true
false
```

On the contrary if you write the same piece of code in other languages
ex. C#
...

Try to write it in C or C++.

(Ran it through Linqpad)
```
bool intersect(int targetX, int targetY, uint targetWidth, uint
targetHeight, int x, int y, uint width, uint height)
{
 return targetX < x + width &&
       x < targetX + targetWidth &&
       targetY < y + height &&
       y < targetY + targetHeight;
}

void Main() {
    intersect(0,0,800,600,     0,150,148,148).Dump();
    intersect(0,0,800,600,     -10,150,148,148).Dump();
}
```

Then it outputs:
```
true
true
```

Is it a bug or is it intended behavior?



This is intended (but surprising, and IMHO bad) behaviour, as D follows C integral promotion rules. (C# does not.) Mixed signed/unsigned operations first convert both arguments to unsigned.
  • ... Is it possible to store different generic types? via Digitalmars-d
    • ... Is it possible to store different generic types? via Digitalmars-d
      • ... Stefan Koch via Digitalmars-d
        • ... Is it possible to store different generic types? via Digitalmars-d
          • ... Timon Gehr via Digitalmars-d
      • ... Ali Çehreli via Digitalmars-d
        • ... Is it possible to store different generic types? via Digitalmars-d
    • ... Timon Gehr via Digitalmars-d
      • ... bauss (wtf happend to my name took some old cached title LOL??) via Digitalmars-d

Reply via email to