3 ms·
C++ can do this very well, no eye bleed is involved.
by cjfd 3y ago
C++ can do this very well, no eye bleed is involved.
- criddell 3y agoI’m skeptical that C++ can manage anything as clear and concise as: type Age is new Natural range 0..150;
- jcelerier 3y agoWell, for the end user it'd look like using age = in_range<int, 0, 150>; Not the end of the world
- 9659 3y agoa little bigger, more complete, example would help. this is something i have never seen in C++. (note C++ is a big language that changes a lot)
- ziotom78 3y agoDoes this really define a new range-checked type? I never used in_range, but it seems a function [1] that you must call whenever you want to do the check on one variable, while in Pascal all the checks are injected automatically every time you modify a variable of that type. [1] https://en.cppreference.com/w/cpp/utility/in_range https://en.cppreference.com/w/cpp/utility/in_range
- Rexxar 3y agoIf I understand correctly the other comments, they just say it's possible to do in c++ with templates/operator overloading/constexpr. It's not an existing type of the standard library. This type could work in simple situations but will probably not be as good as a native type in more complex situations.
- criddell 3y agoThat’s a function, not a type.
- Jtsummers 3y agoIt's a hypothetical. The actual `in_range` template function doesn't work like that at all and would be annoying to use for this if you tried (it returns true/false so you have to wrap all operations in a conditional). jcelerier is suggesting we'd have something like: template<typename T, int min, int max> // probably not int for min/max, but whatever class in_range { ... }; which would be instantiated with `using <my_type_name> = ...;` and would have all the necessary operator overloads and checks. Still, it's only bringing in the runtime, not compile time, checks and can't be "turned off" (well, maybe take a fourth boolean value and have two code paths everywhere that can be optimized down to one if you want to turn off the checks) like it can in Ada (whether that's a good idea or not depends on how well you've proved out your code). It's also introducing all the overhead of an object which isn't necessary in Ada.
- jcelerier 3y ago> Still, it's only bringing in the runtime, not compile time, checks it's doable to have both compile-time and run-time checks: https://gcc.godbolt.org/z/oT3adre86 https://gcc.godbolt.org/z/oT3adre86 some compile-time interval calculus library would enable more check and does not seem much harder to implement than a run-time one.
- pjmlp 3y agoCalling in_range explicilty at every spot, versus letting the compiler do the work, isn't the same.
- lolinder 3y agoFor those of us who don't know C++, would you care to provide a code sample (or a link to one) demonstrating what it would look like?
- cjfd 3y ago#include <stdexcept> template <int min, int max> class Range { public: Range(int i): i(i) { if (i < min) throw std::runtime_error("value lower than minimum " + std::to_string(min)); if (i > max) throw std::runtime_error("value higher than maximum " + std::to_string(max)); } int to_int() const { return i; }; private: int i; }; using Month = Range<1, 12>; int main() { Month month(-1); } terminate called after throwing an instance of 'std::runtime_error' what(): value lower than minimum 1