Defect Report: Is null (when defined as "const int null = 0") a null pointer constant (4.10)?

Defect Report: Is null (when defined as "const int null = 0") a null pointer constant (4.10)?

Post by Lloyd J Le » Mon, 02 Feb 2004 06:40:18



[note: forwarded to C++ committee -sdc ]

In the following code, I expect both "null" and "FALSE" to be null pointer
constants -- and that the code should compile and output the string "int*"
twice to cout:

#include <iostream>

using namespace std;

void foo(int* p)
{
cout << "int*" << endl;
}

int main(void)
{
const int null = 0;
foo(null);
const bool FALSE = false;
foo(FALSE);
}

ISO/IEC 14882-1998 4.10 states:

"An integral constant expression rvalue of integer type that evaluates to
zero (called a null pointer constant) can be converted to a pointer
type."

Stroustrup appears to agree with me -- he states (3rd edition page 88):

"In C, it has been popular to define a macro NULL to represent the zero
pointer. Because of C++`s tighter type checking, the use of plain 0, rather
than any suggested NULL macro, leads to fewer problems. If you feel you must
define NULL, use:
const int NULL = 0;"

However gcc 3.3.1 rejects this code with the errors:

bug.cc:17: error: invalid conversion from `int' to `int*'
bug.cc:19: error: cannot convert `const bool' to `int*' for argument `1'
to `
void foo(int*)'

I have reported this as a bug
( http://www.yqcomputer.com/ ), but the gcc team states
that 4.10 requires that a null pointer constant must be an rvalue -- and no
implicit conversion from an lvalue to an rvalue is required
( http://www.yqcomputer.com/ ):

"a null pointer constant is an integral constant expression rvalue that
evaluates to zero [4.10/1] in this case `null' is an lvalue. The standard
does not specify that lvalue->rvalue decay happens here, so `null' is not a
null pointer constant."

I disagree with the gcc teams interpretation -- I don't see why 3.10 doesn't
apply:

"Whenever an lvalue appears in a context where an rvalue is expected, the
lvalue is converted to an rvalue;"

The insertion of the word rvalue appears to have occurred during
standardization -- it is not present in either Stroustrup 2nd edition or the
3rd edition. Does the committee deliberately intend to exclude an lvalue as
a null pointer constant by adding the word rvalue? If so, it leads to the
rather bizarre fact that "null" is not a null pointer constant, but "null +
0" is!

Lloyd Lewins



[ 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.yqcomputer.com/ ]
 
 
 

1. Is int const myNull 0 a null pointer constant?

2. Is int const myNull = 0 a null pointer constant?

An interesting question has come up in the German newsgroup, and to be
frank, I'm not sure of the correct answer. Is:
int const myNull = 0 ;
a null pointer constant? I had always supposed it was, but careful
reading of the standard (.10/1) says that "A null pointer constant is
an integral constant expression rvalue of integer type that evaluates to
zero." Given the above, "myNull" is not an rvalue, and so not a null
pointer constant. According to other posters, most compilers, including
Comeau, accept it; I seem to have read somewhere that Stroustrup says
it's one somewhere as well. On the other hand, g++ (3.4.0) rejects it,
and the standard does say rvalue, which would seem to support g++.

Am I overlooking something? And if not, is it intentional?

(Also, while I'm at it: can an integral constant expression not have
integer type?)

James Kanze GABI Software http://www.yqcomputer.com/
Conseils en informatique orient objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sard, 78210 St.-Cyr-l'ole, France, +33 (0)1 30 23 00 34

---
[ 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.yqcomputer.com/ ]

3. Pointers are ints, zero is null, and strings are null terminated.

4. WinHttpDownloadFileToMemory(szURLDest, NULL, 0, NULL, NULL, NULL, &downloadBuffer) failed with hr=0x80190194

5. KLAcquireInitialTickets(NULL, NULL, NULL, NULL) returns klParameterErr (19276) on Mac OS X 10.6.3

6. Pointers are ints, zero is null, and strings are null terminated.

7. WinHttpDownloadFileToMemory(szURLDest, NULL, 0, NULL, NULL, NULL,

8. Smart Pointer release() const : it can set the pointer to null with the keyword "const"?

9. Smart Pointer release() const : it can set the pointer to null with the keyword "const"?

10. Implementation-defined null pointer constant?

11. OLEDB: Prepared cmd with NULL input param for NOT NULL INT col fai

12. OLEDB: Prepared cmd with NULL input param for NOT NULL INT c

13. Defect Report: does reinterpret_cast<T*>(0) yield a null pointer?

14. NULL param for NOT NULL INT column fails execution

15. select(0,NULL,NULL,NULL,&t1) used for delay