fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. struct bool_context {
  6. friend T operator&&(T const & lhs, bool rhs) {
  7. return lhs && T(rhs);
  8. }
  9. friend T operator&&(bool lhs, T const & rhs) {
  10. return T(lhs) && rhs;
  11. }
  12. friend T operator||(T const & lhs, bool rhs) {
  13. return lhs || T(rhs);
  14. }
  15. friend T operator||(bool lhs, T const & rhs) {
  16. return T(lhs) || rhs;
  17. }
  18. };
  19.  
  20. struct my_bool : bool_context<my_bool> {
  21. bool value;
  22. my_bool(bool v) : value(v) {}
  23. explicit operator bool() { return value; };
  24. friend my_bool operator&&(my_bool const & lhs, my_bool const & rhs) {
  25. cout << "my_bool::operator&&" << endl;
  26. return lhs.value && rhs.value;
  27. }
  28. friend my_bool operator||(my_bool const & lhs, my_bool const & rhs) {
  29. cout << "my_bool::operator||" << endl;
  30. return lhs.value || rhs.value;
  31. }
  32. };
  33.  
  34.  
  35. int main(int, char**) {
  36. my_bool a = true;
  37. bool b = false;
  38. cout << "a && b => "; a && b;
  39. cout << "b && a => "; b && a;
  40. cout << "a && a => "; a && a;
  41. cout << "b && b => "; b && b; cout << endl;
  42. cout << "a || b => "; a || b;
  43. cout << "b || a => "; b || a;
  44. cout << "a || a => "; a || a;
  45. cout << "b || b => "; b || b; cout << endl;
  46. return 0;
  47. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
a && b => my_bool::operator&&
b && a => my_bool::operator&&
a && a => my_bool::operator&&
b && b => 
a || b => my_bool::operator||
b || a => my_bool::operator||
a || a => my_bool::operator||
b || b =>