Subject: Re: [boost] [histogram] should some_axis::size() return unsigned or int?
From: Hans Dembinski (hans.dembinski_at_[hidden])
Date: 2018-11-29 17:12:08


Dear Jim,

> On 29. Nov 2018, at 13:31, Jim Pivarski <jpivarski_at_[hidden]> wrote:
>
> It seems to me that if other C++ containers use unsigned integers for size(), you should too, for minimal surprise. The issue you raise about the "which bin?" function returning -1 would be solved in a very-strongly typed language like Haskell as an optional<int>. I know that C++ has optional types now, but I don't know how widely they're used or if there's a significant performance penalty. If this wouldn't look too weird in a C++ program and wouldn't slow it down (or needlessly complicate the code), an optional type would describe your intent more fully than -1.

there is boost::optional, which has the semantics of a pointer and can be used to represent a type that stores a value or not.

```
boost::optional o = some_fickle_function();
if (o) { // optional has a value?
  auto value = *o; // "dereference" to get the value
} else {
  // handle case where value is missing
}
```

This is not a good match here, because -1 here does not have the meaning of "value is missing", but it really is the logical index for the virtual bin that spans from -infinity to the lower edge of the first bin in the axis.

Value arrow:
-inf ——————— | ——————— | —————— | —————— |—————————> +inf
      bin -1 bin 0 bin 1 bin 2 bin 3

I think representing the underflow bin with -1 and the overflow bin with the value returned by size() is very intuitive and elegant.

Best regards,
Hans