opDispatch and operator overloads

Steven Schveighoffer schveiguy at yahoo.com
Tue May 21 08:31:57 PDT 2013


On Mon, 20 May 2013 11:15:32 -0400, John Colvin  
<john.loughran.colvin at gmail.com> wrote:

> struct S {
> 	auto opDispatch(string s)(A i){}
> }
>
> struct A {}
>
> void main() {
> 	S s;
> 	A a;
> 	s + a; //Error: incompatible types for ((s) + (a)): 'S' and 'A'
> }
>
> It would be really nice if opDispatch could catch missing operator  
> overloads.

Not sure this can work.  opDispatch takes a string identifying the  
function name, but opBinary is a template that ALSO needs a string.

I suppose the string parameter to opDispatch could be the explicit  
instantiation:

s.opDispatch!"opBinary!\"+\""(a)

but that would seem extremely difficult to handle, you'd kind of need a  
parser to deal with it.

what is the problem with just defining opBinary to catch missing operator  
overloads?

> Also, would it be a good idea to have free functions of all the  
> operators (opOpAssign etc...) for builtin types somewhere? It's  
> occasionally useful in generic wrappers.

The name of operator overloads are the actual operators.  The opOpAssign,  
etc, are hooks.  Use the actual operators in generic code.  A use case to  
discuss may be more helpful.

-Steve


More information about the Digitalmars-d mailing list