d的中缀操作

struct Operator(alias fn, string operator = "/")
{
    static auto opBinaryRight(string op : operator, T...)(T value1)
    {
        struct Result
        {
            auto opBinary(string op : operator, U...)(U value2)
                if (__traits(compiles, fn(value1, value2)))
            {
                return fn(value1, value2);
            }
        }

        Result result;
        return result;
    }
}

void main()
{
    import std.algorithm.comparison;
    alias min = Operator!(std.algorithm.comparison.min, "%");
    assert(1 %min% 3 == 1);
}
posted @ 2022-10-15 08:53  zjh6  阅读(14)  评论(0)    收藏  举报  来源