
"Rob Stewart" <stewart@sig.com> wrote in message news:200509162032.j8GKWh51025673@shannonhoon.balstatdev.susq.com... Consider that the class is used to model, say, permitted voltages in an electonic circuit. Say that an input on the circuit is constrained by only being able to support voltages of 2-5V. (If you connect two batteries in series, the voltages add.) If each battery's voltage is expressed with a checked integer of the 2-5V range, and their voltages are summed, your result would be a checked integer with a 4-10V range. That exceeds the permitted input voltage range and two batteries of 4V each would blow the input. How would you handle that in your model?
given a constrained value type, CV<min, max, policy> and the following: CV<2, 5, some_policy> batteryA, batteryB; you could decide either to do: CV<2, 5, compiletime_policy> input = batteryA+batteryB; // compiler will kindly alert you to the fact that there is a potential problem here or, if you are confident the potential problem doesn't occur: CV<2, 5, assert_policy> input = batteryA+batteryB; // at runtime, if something bad happens, it will assert
In my approach, the result would be a value with a 4-5V range. That fits within the 2-5V range and so a successful sum of battery voltages doesn't exceed the range of the input.
Unless I've missed something, your approach requires a runtime check to see whether the actual result of the addition exceeds the input voltage range during some later computation or comparison. My approach catches the range error during the addition.
-- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer; _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost