Skip to content

Instantly share code, notes, and snippets.

@schveiguy
Last active Jun 14, 2022
Embed
What would you like to do?
D1 operator overloads
mixin template D1Ops() {
// binary operations
static foreach(op, d1;
["+" : "opAdd", "-" : "opSub", "*" : "opMul", "/" : "opDiv",
"%" : "opMod", "&" : "opAnd", "^" : "opXor", "|" : "opOr",
"<<" : "opShl", ">>" : "opShr", ">>>" : "opUShr", "~" : "opCat",
"in" : "opIn"]) {
static if(__traits(hasMember, typeof(this), d1))
alias opBinary(string s : op) = mixin(d1);
// _r version
static if(__traits(hasMember, typeof(this), d1 ~ "_r"))
alias opBinaryRight(string s : op) = mixin(d1 ~ "_r");
// assignment (everything except opIn)
static if(op != "in" && __traits(hasMember, typeof(this), d1 ~ "Assign"))
alias opOpAssign(string s : op) = mixin(d1 ~ "Assign");
}
// unary operations
static foreach(op, d1;
["-" : "opNeg", "~" : "opCom", "++" : "opPostInc",
"--" : "opPostDec", "*" : "opStar"]) {
static if(__traits(hasMember, typeof(this), d1))
alias opUnary(string s : op) = mixin(d1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment