Topic: explicit user-defined conversion operators
Author: sean@delta.com (Sean L. Palmer)
Date: 1996/09/20 Raw View
> Actually, this situation is the first thing I've seen that would call for
> use of an "explicit" conversion to type. Ordinarily you're better off
> writing a "named" function to provide explicit conversion to type (as
> opposed to implicit conversion). Here is a place where "explicit" would
be
> *very* handy...
>
> class ios {
> // ...
> explicit operator bool() const {return good();}
> // ...
> };
>
> In this case, you could convert an ios to bool only explicitly and not in
> an implicit expression conversion. Consider the bool "argument" to
> conditionals an explicit conversion to bool. Then
>
> if (cin) { /* ... */ }
>
> is legal at compile time, but
>
> int x, y = 10;
> x = y + cin;
>
> or
>
> int x;
> cout >> x;
>
> is not. (Am I right here? That is, cout >> x does involve an implicit
> conversion, right?) Is it too late to incorporate this idea into the WP,
> since it has a precedent in explicit constructors and it satisfies the
> "Principle of least astonishment" in a way the current WP doesn't? My
only
> concern is that considering the boolean in a conditional as an explicit
> conversion may have undesirable side-effects.
YAY! someone else agrees with me. I have been trying to push for this very
thing for quite a while now. It disappointed me to no end to find out that
explicit only worked on constructors, not user-defined conversions.
This problem occurs in many situations. Mainly with templates.
I have a good example.
I've written a fixed-point class which is mainly for REPLACING the use of
floating point. I can emulate every aspect and operator and library
function that's legal for double for my fixed-point class, but I CAN NOT
use my fixed-point class interchangeably with double in any code that's not
explicitly designed to use it, because you can do this:
double d;
int i=int(d);
double j=double(d);
but you can't do this:
fixed d;
int i=int(d); //ambiguity
double j=double(d); //ambiguity
You also can't provide a conversion *function* named int or double because
those are reserved words. So you end up having to use an Int() and Double()
function and also providing those same named functions for any other types
you may use instead of fixed, and then only use Int(x) and Double(x) in
your templates.
IT SUCKS!
I want to write this class so it is TRANSPARENTLY INTERCHANGEABLE with
double. But lacking the explicit keyword, I can't do it. I'd provide an
implicit UDC to double and explicit UDC's to int, long, bool, float, etc,
that way there'd be ambiguity if trying to implicitly cast to int, which
loses information, but not to double, which can handle it. But standard
explicit casts would still work, and you could take almost any template
class or function that was designed to deal with numeric types (namely
double), and plug in fixed instead, and it would work!
It would be great if the committee allowed explicit on user-defined
conversions.
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]