From -1426138978323380274 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f78e5,6a0e273cd04a6fa1 X-Google-Attributes: gidf78e5,public X-Google-ArrivalTime: 2002-01-10 10:42:01 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!colt.net!dispose.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail From: prophet36NOSPM@hotmail.com (Chris McClelland) Newsgroups: comp.std.c++ Subject: Re: new meaning of explicit keyword Date: Thu, 10 Jan 2002 18:40:58 GMT Organization: http://groups.google.com/ Approved: Fergus Henderson , moderator of comp.std.c++ Message-ID: <92ce342f.0201100428.49e3dd92@posting.google.com> References: X-Trace: mail2news.demon.co.uk 1010688063 mail2news:2508 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) Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit NNTP-Posting-Date: 10 Jan 2002 12:28:24 GMT Lines: 77 Xref: archiver1.google.com comp.std.c++:8932 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(b); // Fine... float e = (float)b; // Also fine... } Sean Parent has provided a nice workaround for those of us who have the luxury of standard-compliant compilers: http://groups.google.com/groups?q=explicit+cast&hl=en&selm=B718ED17.1502D%25sparent%40adobe.com&rnum=3 - Chris "Sektor van Skijlen" wrote in message news:... > For now, the "explicit" keyword allows a type CONSTRUCTOR to > disallow implicit conversions into this type. This keyword could be > also used to signify, that this implicit conversion is not allowed for > the object initialization. For example: > > int x = 'c'; // allowed > explicit int x = 'c'; // not allowed! > explicit int x = int( 'c' ); // allowed > > This feature could be mostly used in a situation, where the implicit > conversion, granted by standard, is a disadvantage in some cases. > For example, one of std::string's constructors take two arguments, > which mean an amount of characters and a character. The > disadvantage is that the compiler won't see any error if you pass > arguments in wrong order. This will be passed, because there > will be ALLOWED conversion from char to size_t and in reverse. > This bug is always hard to detect. > > Using this feature you can state: > > string( size_t size, explicit char ch ); > > And then this: > > string s( 'c', 12 ); > > won't compile, because "12" can't be implicitly converted to char. > However it can be worked around: > > string s( 'c', char( 12 ) ); > > > Regards, > -- > (( > )) ektor > > > --- > [ 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 ] --- [ 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 ]