3 ms·
I haven't seen anyone mention the main reason I end up using signed: to allow for an "out-of-bounds" value to indicate "value not set". As long as the N-1 bit v
by PaulDavisThe1st 5y ago
I haven't seen anyone mention the main reason I end up using signed: to allow for an "out-of-bounds" value to indicate "value not set". As long as the N-1 bit version of the integer is large enough, not having to use an additional variable to indicate "var not set" is really quite lovely. One could potentially replace the use of a negative sentinel value with a maximum sentinel value, and I've done this on occasion too (i.e. if (var == 2^N-1) var-is-not-set). But the cognitive distinction between negative == not set, positive == valid value is easier to deal with (for me, at least).
- ncmncm 5y agoThis the proper place for std::optional<T>.
- PaulDavisThe1st 5y agothat has quite the overhead for something that can be done using a single bit of the actual value. roughly half the programming i do is high-performance real-time constrained. not really appropriate in that context.
- ncmncm 5y agoThere is a proper place for everything, and others for everything else. You can make a zero-overhead analog of optional<int>, for such cases, for clarity and safety. (Unless you are also trying to present a C API for cross-language compatibility...) But it is tricky to determine the exact amount of handholding to provide for such a type, and extra work to remember exactly what it is. We all know how ints work.