Operator Overloading - Only for Classes and Structs?

matheus matheus at gmail.com
Sun Jun 22 15:04:27 UTC 2025


Hi, could someone please tell me why operador overloading 
(https://dlang.org/spec/operatoroverloading.html) is relegated 
only to classes and structs?

"1 Operator overloading is accomplished by rewriting operators 
whose operands are class or struct objects into calls to 
specially named members. No additional syntax is used."

I'd like to overload "~" to concatenate a string with a integer 
and do something else,  like:

   auto s = "1" ~ 2;

But what I've tried so far didn't work.

If that constraint (Classes and Structs) is true, why?

Thanks,

Matheus.


More information about the Digitalmars-d-learn mailing list