Topic: Is `int *x = false;' valid?


Author: john@mail.interlog.com (John R MacMillan)
Date: 1998/09/28
Raw View
|Suppose foo.cpp contains just the one line:
|
|    int *x = false;

[ Is the above allowed? ]

I must admit I was surprised by this, but my reading of the
November 1997 Draft is that this is legal.  The value "false" is
a boolean literal, and bool is an integer type, so it qualifies
as an integral constant expression with value 0, and hence as a
null pointer constant.
--
To reply by mail, please remove "mail." from my address -- but please
send e-mail or post, not both


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1998/09/29
Raw View
syd@quango.force9.co.uk wrote:
>
>
>
> Should VC5 have also displayed this error when the line said `false'?

I think it's technically legal, although I would have much
preferred to see a warning.

bool's are integral types.  false is therfore an integral
constant 0, and thus is a legal null pointer.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: syd@quango.force9.co.uk
Date: 1998/09/28
Raw View
Suppose foo.cpp contains just the one line:

    int *x = false;

then Microsoft VC5 SP3 displays no warnings or errors when compiling
using the command line:

    cl -W4 -c foo.cpp

However gcc 2.7 when compiling with:

    gcc -Wall -c foo.cpp

displays the error:

    foo.cpp:1: initialization to `int *' from `bool'

It looks like VC5 interprets the line as if it said:

    int *x = 0;

and then allows the cast to `int *'.  Note that changing the `false'
to `true' and then recompiling using VC5 displays the following error:

foo.cpp(1) : error C2440: 'initializing' : cannot convert from 'const bool'
             to 'int *'
             Conversion from integral type to pointer type requires
             reinterpret_cast, C-style cast or function-style cast

Should VC5 have also displayed this error when the line said `false'?

Geoff
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]