Topic: explicit (was typedef typename)
Author: herbs@interlog.com (Herb Sutter)
Date: 1995/07/21 Raw View
[Note: Follow-ups to comp.lang.c++]
In article <3unfl1$47q@etca.etca.fr>,
chris@alofi.etca.fr (Christian Millour) wrote:
[edited for brevity]
>
>In other words, I dont understand the purpose of using explicit in
>the definition of the complex specializations
>
>class complex<float> {
>explicit complex<const complex<double>&>;
>explicit complex<const complex<long double>&>;
>};
Notice that these are all copy ctors and the outer brackets are round, i.e.
"complex(const complex<double>&)", not angled as above and below.
>class complex<double> {
>complex<const complex<float>&>;
>explicit complex<const complex<long double>&>;
>};
>
>class complex<long double> {
>complex<const complex<float>&>;
>complex<const complex<double>&>;
>};
Recall a copy ctor defines an implicit conversion. Notice here that only the
narrowing conversion are made "explicit"; this means that the user can't
implicitly supply a complex<double> where a complex<float> is expected,
because that would lead to an invisible (and possibly incorrect) narrowing
from double to float, losing precision. If the user really wants to do it, he
can, but he has to write "complex<double>(myComplexFloat)" explicitly.
IOW, this part of the standard library chooses to make the user explicitly say
when he wants a narrowing conversion.
Follow-ups to comp.lang.c++.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herb Sutter 2228 Urwin, Ste 102 voice (416) 618-0184
Connected Object Solutions Oakville ON Canada L6L 2T2 fax (905) 847-6019
Author: herbs@interlog.com (Herb Sutter)
Date: 1995/07/21 Raw View
In article <3uoe42$e98@steel.interlog.com>, I wrote:
>
>Recall a copy ctor defines an implicit conversion. Notice here that only the
>narrowing conversion are made "explicit"; this means that the user can't
>implicitly supply a complex<double> where a complex<float> is expected,
>because that would lead to an invisible (and possibly incorrect) narrowing
>from double to float, losing precision. If the user really wants to do it, he
>can, but he has to write "complex<double>(myComplexFloat)" explicitly.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
My typo. It's the other way around, of course.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herb Sutter 2228 Urwin, Ste 102 voice (416) 618-0184
Connected Object Solutions Oakville ON Canada L6L 2T2 fax (905) 847-6019
Author: chris@alofi.etca.fr (Christian Millour)
Date: 1995/07/19 Raw View
In article <3uj86v$egn@engnews2.Eng.Sun.COM>, clamage@Eng.Sun.COM (Steve Clamage) writes:
|> <deletia>
|>
|> That is why the 'explicit' keyword was added. A constructor declared
|> 'explicit' can be used to perform a type conversion only when explicitly
|> invoked. Example:
|> class miles {
|> public:
|> explicit miles(unsigned long);
|> ...
|> };
|> void foo(miles);
|> ...
|> foo(10); // ERROR, no matching function
|> foo(miles(10)); // OK, explicit type conversion
|>
regarding the `explicit' keyword, could someone shed some light on the
rationale for its use in the definition of the std complex classes
(26.2.2, [lib.complex.special]) ?
complex<float> c1;
complex<float> c2;
c1 = c2; // won't compile
c1 = complex<float>(c2); // OK but heavy (IMHO)
TIA,
--chris@etca.fr