Topic: Default parameters and implicit type conversions
Author: "V.A.Neelesh" <v_a_neelesh@hotmail.com>
Date: 1997/12/03 Raw View
Brian McKellar wrote:
>
> Should the third F() compile? (FWIW tested with MS VC++ v5)
> brian
Yes it should compile coz a character pointer can be initialized to
'0'(NULL). eg:- char * name=0 is valid;
Neelesh
---
[ 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 ]
Author: Brian McKellar <bcm@heidelbg.ibm.com>
Date: 1997/11/27 Raw View
Should the third F() compile? (FWIW tested with MS VC++ v5)
brian
void F (char* s="parm", bool b=true) { /*...*/ };
void main (void)
{
F (); // OK implicit parms used
F ("my parm", false); // OK explicit parms given
F (false); // BUG? false converted to 0 for s
F (true); // OK did not compile
}
--
Brian McKellar bcm@vnet.ibm.com, IBM European Networking Center
---
[ 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 ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1997/11/29 Raw View
Brian McKellar <bcm@heidelbg.ibm.com> wrote:
: Should the third F() compile? (FWIW tested with MS VC++ v5)
: brian
: void F (char* s="parm", bool b=true) { /*...*/ };
: void main (void)
: {
: F (); // OK implicit parms used
: F ("my parm", false); // OK explicit parms given
: F (false); // BUG? false converted to 0 for s
There is a standard conversion from bool to pointer. Since there is
no other option available, it seems reasonable while absurd.
: F (true); // OK did not compile
This may be a bug since it did not convert true to a pointer with
value one. Another absurdity. But there must be rules. Don't push
them too far.
: }
John
---
[ 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 ]
Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/11/29 Raw View
jpotter@falcon.lhup.edu (John Potter) writes:
>Brian McKellar <bcm@heidelbg.ibm.com> wrote:
>
>: Should the third F() compile? (FWIW tested with MS VC++ v5)
>: brian
>
>: void F (char* s="parm", bool b=true) { /*...*/ };
>
>: void main (void)
>: {
>: F (); // OK implicit parms used
>: F ("my parm", false); // OK explicit parms given
>: F (false); // BUG? false converted to 0 for s
>
>There is a standard conversion from bool to pointer.
I think you are mistaken. There is a standard conversion from pointer
to bool, but there is no standard conversion from bool to pointer.
There is however a standard conversion from null pointer constant to
pointer. The question is whether `false' is a null pointer constant.
I believe that according to the wording in the Nov 96 and Nov 97 drafts,
it is.
First, we need to consult 4.10, to find the definition of null pointer
constant.
| 4.10 Pointer conversions [conv.ptr]
|
| 1 A null pointer constant is an integral constant expression
| (_expr.const_) rvalue of integer type that evaluates to zero. A null
| pointer constant can be converted to a pointer type; [...]
Next, we need to consult 3.9.1, to find the definition of "integer type".
| 3.9.1 Fundamental types [basic.fundamental]
|
| 7 Types bool, char, wchar_t, and the signed and unsigned integer types
| are collectively called integral types.20) A synonym for integral type
| is integer type.
FWIW, I think this is a bug in the draft --- values of type bool, char,
and wchar_t should never be null pointer constants. A possible fix
would be to insert "signed or unsigned" before "integer type" in 4.10/1.
I guess it is a bit late now... I think changes like this have to be
handled as defect reports. In the mean time, I strongly recommend that
all compiler vendors make sure their compilers issue warnings for null
pointers of type bool, char, or wchar_t.
--
Fergus Henderson <fjh@cs.mu.oz.au> WWW: <http://www.cs.mu.oz.au/~fjh>
Note: due to some buggy software and a (probably accidental)
denial-of-service attack, any mail sent to me or comp.std.c++
Tue Nov 25 20:00:00 UTC (6am Wed, local time)
and Wed Nov 26 06:00:00 UTC (4pm, local time)
may have gone into the bit-bucket. Please re-send it.
---
[ 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 ]