fork(2) download
  1. #include <iostream>
  2. #include <functional>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. template<
  7. typename T,
  8. T minVal,
  9. T maxVal,
  10. typename Compare = less<T>>
  11. class RangedVal{
  12. public:
  13. using value_type = T;
  14. using compare_type = Compare;
  15. static constexpr value_type min = minVal;
  16. static constexpr value_type max = maxVal;
  17. static constexpr compare_type compare = compare_type();
  18.  
  19. static constexpr bool check(value_type val){
  20. return compare(min, val) && compare(val, max);
  21. }
  22. private:
  23. value_type value;
  24. public:
  25. constexpr RangedVal(value_type val): value(check(val)? val : throw val){}
  26.  
  27. constexpr operator value_type() const{
  28. return value;
  29. }
  30. };
  31.  
  32. int main() {
  33. RangedVal<int, 0, 6> val(3);
  34. cout << val << endl;
  35. return 0;
  36. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
3