fork download
  1. #include <limits>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. template <typename T>
  8. inline bool is_between(const string &str,
  9. T min = numeric_limits<T>::min(),
  10. T max = numeric_limits<T>::max()) noexcept
  11. {
  12. try {
  13. long double value = stold(str);
  14.  
  15. if (value < min || value > max)
  16. return false;
  17. } catch (...) {
  18. return false;
  19. }
  20.  
  21. return true;
  22. }
  23.  
  24. #include <iostream>
  25.  
  26. int main()
  27. {
  28. cout << boolalpha;
  29.  
  30. // UINT64_MAX -> true
  31. cout << is_between<uint64_t>("18446744073709551615") << endl;
  32.  
  33. // UINT64_MAX + 1 -> false
  34. cout << is_between<uint64_t>("18446744073709551616") << endl;
  35. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
true
false