fork(1) download
  1. #include <iostream>
  2.  
  3. struct Java_boolean
  4. {
  5. explicit constexpr Java_boolean( bool b = false ) : value(b) {}
  6.  
  7. using Java_bool_t = void (Java_boolean::*)() const ;
  8. operator Java_bool_t() const { return value ? &Java_boolean::not_integral : nullptr ; }
  9.  
  10. int to_int() const { return value ; }
  11. bool operator== ( Java_boolean that ) const { return value == that.value ; }
  12. bool operator!= ( Java_boolean that ) const { return value != that.value ; }
  13. Java_boolean operator! () const { return Java_boolean { !value } ; }
  14.  
  15. private:
  16. bool value ;
  17. void not_integral() const {}
  18.  
  19. template < typename STREAM > inline friend
  20. STREAM& operator << ( STREAM& stm, Java_boolean b )
  21. { return stm << "Java_" << ( b.value ? "true" : "false" ) ; }
  22. };
  23.  
  24. constexpr Java_boolean Java_true { true } ;
  25. constexpr Java_boolean Java_false { false } ;
  26.  
  27. int main()
  28. {
  29. Java_boolean flag { Java_true } ;
  30. if( flag ) std::cout << "ok\n" ;
  31. if( flag == Java_true ) std::cout << "ok\n" ;
  32. std::cout << flag << '\n' ;
  33.  
  34. int i = flag.to_int() ; // fine
  35. //int j = flag ; // error
  36.  
  37. flag = Java_boolean(i) ; // fine
  38. // flag = i ; // error
  39. }
  40.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
ok
ok
Java_true