Operator overloading question

bearophile bearophileHUGS at lycos.com
Mon Jan 21 04:27:33 PST 2013


Nathan M. Swan:

> The correct keyword is "opBinary", not "opbinary".

The compiler must give an error message easy to understand in 
similar wrong cases.


> http://dpaste.1azy.net/b73ef2cd

This is your code:

	Arithmetic opbinary(string op)(Arithmetic rhs)
	{
		static if(op == "+") return add(rhs);
		static if(op == "*") return mul(rhs);
		else static assert(0, "Operator "~op~" not implemented");
	}


I like to add template constraints, where possible:

Arithmetic opbinary(string op)(Arithmetic rhs)
const pure nothrow if (op == "+" || op == "*")

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list