Can the user define how primitives cast between primitives? Or how they cast to user-defined types? (“Implicit casting constructor”? yikes)
This is a hypothetical question and it’s hard to imagine a usage but perhaps one might want to affect how ints cast to bool.
You can’t change how built-in types behave. You’re not allowed to break the language like that, much like you cannot overload a new
operator+forints that does something else.The language needs to be able to make certain basic guarantees so that libraries can be written that can rely on basic behaviour!
You can convert any type to a user-defined type by providing a constructor that’s callable with one argument, e.g.:
Now you can say,
T x; Foo y = x;etc.You can also do the reverse and provide conversions from your class:
Now you can also say,
S s = y;.