bogorodniy's blog

By bogorodniy, history, 4 years ago, translation, In English

I wondered for a long time how to make different user-operators. For example write this x minEqual y instead of x = min(x, y)

So recently I've done this, and decided to share it with you.

#include <bits/stdc++.h>

using namespace std;
struct mineq_operator
{
    int tempVal=1000;
    inline mineq_operator operator<<(int& x)
    {
        this->tempVal = x;
        return *this;
    } inline mineq_operator operator>>(int& x)
    {
        x = min(x, tempVal);
        return *this;
    }
};
mineq_operator __me;
inline void operator<<(int& x, mineq_operator& b)
{
    b.tempVal = x;
}
#define minEqual <<__me;__me>>
signed main()
{
    int x = 3, y = 2;
    y minEqual x;
    cout << x << ' ' << y << endl;
}

For use it like x = min(x, y); just write y minEqual x;

And this in compressed format:

#include <bits/stdc++.h>

using namespace std;
struct mineq_operator {int tempVal=1000;inline mineq_operator operator<<(int& x) {this->tempVal = x;return *this;}inline mineq_operator operator>>(int& x) {x = min(x, tempVal);return *this;}};
mineq_operator __me; inline void operator<<(int& x, mineq_operator& b) {b.tempVal = x;}
#define minEqual <<__me;__me>>
signed main() {
  int x = 3, y = 2;
    y minEqual x;
    cout << x << ' ' << y << endl;
}

Full text and comments »

  • Vote: I like it
  • +61
  • Vote: I do not like it