Operator overloading through UFCS doesn't work

Maxim Fomin maxim at maxim-fomin.ru
Sat Oct 13 04:50:39 PDT 2012


On Saturday, 13 October 2012 at 10:00:22 UTC, Tommi wrote:
> On Saturday, 13 October 2012 at 09:50:05 UTC, Jonathan M Davis 
> wrote:
>> It is most definitely _by design_ that you cannot
>> overload operators except as member functions.
>
> I don't understand this design choice then. I don't see any 
> problem in allowing UFCS operators. Because of the way UFCS 
> works, it's guaranteed that there can't be any operator 
> hijacking.

I think implementing UFCS operator overloading is problematic. 
Firstly, you want to put this language addition too far. 
Secondly, compiler needs to know whether operator was overloaded 
or not. If it knows, it generates code to call "opSomething", if 
nor - it just doesn't generate anything. Now, imagine what would 
happen if you write in some module "free" function, supposed to 
hijack operator overloading method of class or struct in another 
module. If you compile them together, operator would be 
overloaded, if separately - nothing would happen. This means that 
operator overloading would depend on with what you compile your 
module - sometimes nothing would be overloaded, sometimes it 
would be with one function, sometimes with another. Thirdly, I 
see no reason in allowing it - for what purpose does you proposal 
service for?


More information about the Digitalmars-d-learn mailing list