From -7902651614303735909
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,6a0e273cd04a6fa1
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2002-01-10 12:49:01 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
From: "Andrea Ferro" <AndreaF@UrkaDVD.it>
Newsgroups: comp.std.c++
Subject: Re: new meaning of explicit keyword
Date: Thu, 10 Jan 2002 20:48:55 GMT
Organization: Urka s.r.l.
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <bQm%7.65$dd2.1604@nreader3.kpnqwest.net>
References: <a1hsph$p4k$1@news.tpi.pl> <92ce342f.0201100428.49e3dd92@posting.google.com> <3C3DE3E6.F27385B2@acm.org>
X-Trace: mail2news.demon.co.uk 1010695740 mail2news:3527 mail2news mail2news.demon.co.uk
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-15"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
NNTP-Posting-Date: Thu, 10 Jan 2002 21:28:55 MET
Lines: 70
Xref: archiver1.google.com comp.std.c++:8946

"Pete Becker" <petebecker@acm.org> wrote in message
news:3C3DE3E6.F27385B2@acm.org...
> Chris McClelland wrote:
> >
> > Interesting.
> >
> > I'd like to be able to use explicit on cast operators, to force an
> > explicit cast. Like this:
> >
> > class X {
> > public:
> >   X(float val) {...}
> >   explicit operator float() {...}
> > };
> >
> > int main() {
> >     float a = 10.3f;
> >     X b = a;  // Fine. Use X(float)
> >     float c = b;  // Error!!
> >     float d = static_cast<float>(b);  // Fine...
> >     float e = (float)b;  // Also fine...
> > }
> >
>
> What benefit does a mandatory cast have over a function call?
>
> float e = b.to_float();

I think the biggest one would be the possibility to use it in templates.
That is to make the source type a template parameter.

Assuming we want assigmenets marked A to be valid for classes where the
cast operator is not marked explicit (inluding of course all classes so
far developed and predefined types) but fail for classes we develop with
the explicit mark on the cast operator, while cases marked B should be
always valid, you see the difference in templated code like:

float e1,e2;
T b;
e1 = b;    // A
e2 = static_cast<float>(b); // B

and

float e1,e2;
T b;
e1 = b;    // A
e2 = b.to_float(); // B

Of course B does not work in the second case for any type that is
already there.

As Chris noted a brilliant but partial solution (does not account for
existing algorithms) is the explicit_cast<> template by Sean Parent.

Note: Im not defending any extension of the meaning of "explicit". I've
not given it sufficient brain time-slices to take position pro or versus
it. I'm trying to answer your question on potential benefits of the
mandatory cast syntax over the conversion function.

--

Andrea Ferro

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]



