fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. using namespace std;
  5. template <typename T>
  6. struct Validated
  7. {
  8. private:
  9. T m_value;
  10. bool m_isValid;
  11.  
  12. public:
  13. Validated() : m_value(), m_isValid(false) {}
  14.  
  15.  
  16. explicit Validated(T const& value) : m_value(value), m_isValid(true) {}
  17.  
  18. template <typename Y=T,typename std::enable_if<!std::is_same<Y,bool>::value,int>::type =0>
  19. explicit Validated(bool const& value) = delete;
  20.  
  21. explicit Validated(bool isValid, T const& value) : m_value(value), m_isValid(isValid) {}
  22. explicit Validated(bool isValid, T&& value) : m_value(value), m_isValid(isValid) {}
  23.  
  24. bool IsValid() const { return m_isValid; }
  25. T const& Value() const { return m_value; }
  26. };
  27.  
  28. int main() {
  29. Validated<bool> v(true);
  30. //Validated<int> v2(true); //fails
  31. Validated<int> v2(2);
  32. return 0;
  33. }
Success #stdin #stdout 0s 4548KB
stdin
Standard input is empty
stdout
Standard output is empty