I’ve come across this blog article: Implicit Conversion Operators are Bad. The article discourages the use of implicit conversion with reference types. The article describes problems caused by implicit conversions of reference types, which boils down to the following
[...] main question of this post: why are implicit conversion operators bad on reference types? [...] By adding an implicit conversion operator to a reference type, you have now introduced value semantics to assignments – but only from one type to another.
My question, however, is about value types. In the beginning, the article has got this statement:
Implicit conversion operators are incredibly important to the language, but only for value types.
The article doesn’t go into details on how to use implicit conversion with value types. That is what I’m wondering about. Here's my present thinking:
- The conversion operator is on a value type, and it returns a value type. Implicit - okay.
- The conversion operator is on a reference type, and it returns a value type. Implicit – okay.
- The conversion operator is on a value type, and it returns a reference type. (That would be a somewhat bizarre, but would it be wrong?) Implicit – not okay.
- The conversion operator is on a reference type, and it returns a reference type. Implicit – not okay.
In a more compact form:
- If it returns a value type, an implicit conversion operator is okay on both value type and reference type.
- If it returns a reference type, implicit conversion is not okay.
Is this correct? Is this complete?
p.s. For the most part, I work mostly with C and C++, where I have fine control over things like const, pointers, references. Of course, that comes at a risk of contracting nasty pointer-related bugs.