fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. template<typename T>
  6. struct identity { typedef T type; };
  7.  
  8. template<typename T>
  9. using identity_t = typename identity<T>::type;
  10.  
  11. template<typename X>
  12. void debugValidateParameter( X aValueToCheck, identity_t<X> aLowerLimit, identity_t<X> aUpperLimit)
  13. {
  14. if( (aValueToCheck > aUpperLimit) || (aValueToCheck < aLowerLimit) )
  15. {
  16. cout << "ERROR: ValidateParameters, aValueToCheck = " << aValueToCheck << ", aLowerLimit= " << aLowerLimit << " , aUpperLimit= " << aUpperLimit << endl;
  17. //throw(std::out_of_range("Invalid Range"));
  18. }
  19. }
  20.  
  21.  
  22. int main() {
  23. unsigned int a = 50;
  24. debugValidateParameter(a, 0, 100);
  25. debugValidateParameter(a, -100, 100); // Validation will fail UNINTENTIONALLY, since -100 is implicitly converted to unsigned int which becomes a very large positive value
  26.  
  27. int b = -50;
  28. debugValidateParameter(b, 0, 100);
  29. debugValidateParameter(b, -100, 100); // Validation will fail intentionally
  30. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
ERROR: ValidateParameters, aValueToCheck = 50, aLowerLimit= 4294967196 , aUpperLimit= 100
ERROR: ValidateParameters, aValueToCheck = -50, aLowerLimit= 0 , aUpperLimit= 100