On Friday, 6 September 2013 at 13:01:14 UTC, Dicebot wrote:
On Friday, 6 September 2013 at 11:32:11 UTC, Benjamin Thaut
wrote:
Its only a source of troubles in C++ because it is the default
behavior. But if you design a library it can make the usage of
your api easier and also you have a few more options to stay
backwards compatible with your old api.
Probably. But what is the gain? `foo(Foo(5))` looks better than
`foo(5)` to me in every possible way.
For example, use case that justifies operator overloading
(despite the danger) in my eyes is ability to replace built-in
types with custom ones. What is the similar rationale for
implicit conversion?
In a few cases it's really useful.
Assume that you have user-defined type, for example BigInt.
You must implement A LOT OF "foo" functions which works only
whith BigInt, like RSA, DSA and etc. For example, 100-500
different functions.
Do you still want to write every time `foo(BigInt(5))`?
(Remember: you have to use only BigInt).
So, the short answer is the same as for operator overloading: in
my eyes is ability to replace built-in types with custom ones.
Yes, it can be dangerous, and we must to provide explicit cast by
default. But in many cases it can simplify a life and a code.