| if ( i < x < j) { ....} |
| if (i < x && x < j) |
| template<typename T> struct compare { bool result; const T& rsh; compare(bool res, const T& v): result(res), rsh(v){} template<typename U> inline friend compare<U> operator < (const compare& lsh, const U& rsh) { return compare<U>(lsh.result && lsh.rsh < rsh, rsh); } bool operator!() const { return !result; } operator bool() const { return result; } }; |
| >,>=, <=, ==, != |
| using namespace std; int main() { int i = 30; int j = 40; if ( compare<int>(true, 2) < i < j < 50) { cout << "OK." << endl; } if ( compare<int>(true, 2) < i < 35 < 38 < j < 42) { cout << "OK." << endl; } if ( compare<int>(true, 2) < i < j < 40) { cout << "ooo." << endl; } return 0; } |
关注此文的读者还看过: