
Hi,
Does boost have some sort of null_ptr class that allows you to do portable
NULL comparisons? For example, the following isn't portable:
char* foo = 0;
if( foo != 0 )
{
}
But this is portable:
char* foo = reinterpret_cast

On Jan 2, 2008 8:02 PM, Robert Dailey
Hi,
Does boost have some sort of null_ptr class that allows you to do portable NULL comparisons? For example, the following isn't portable:
char* foo = 0; if( foo != 0 ) { }
Why is this not portable? [snip] -- Felipe Magno de Almeida

They are unrelated types.
On Jan 2, 2008 4:05 PM, Felipe Magno de Almeida
Hi,
Does boost have some sort of null_ptr class that allows you to do
On Jan 2, 2008 8:02 PM, Robert Dailey
wrote: portable NULL comparisons? For example, the following isn't portable:
char* foo = 0; if( foo != 0 ) { }
Why is this not portable?
[snip]
-- Felipe Magno de Almeida _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 02/01/2008, Robert Dailey
They are unrelated types.
But 0 is a compile-time integral constant with value 0, and is therefore implicitly convertible to any null pointer. Why do you think the comparison is any less portable than the initialization? And the reinterpret_cast is actually less portable, since it assumes that the null pointer is represented by a null bit sequence. The correct way to cast 0 to a null pointer is through static_cast (or boost::implicit_cast).

I can't really agree or disagree, as my information comes from an article I
read a long time ago. It stated that the portable way to assign NULL to a
pointer is to first cast it to the type of the lvalue. At the very least I
would expect (out of pessimism) SOME compiler in the world today to throw a
warning when it sees this:
class foo {};
foo* myfoo = 0;
However, I haven't personally tried this. In all honesty I don't see any
reason why the compiler would ever need to complain about this since I don't
foresee any problems with assigning an integral constant of 0 to pointer
types, or even using them during comparisons. However I guess if you guys
say that it is portable, I suppose I should just save myself some executable
size and avoid the use of nullptr. At this point the only real use I see in
a nullptr class/struct is for readability.
Where in the standard does it state that assigning an integral constant to a
pointer type is legal? Perhaps there is some more generic rule in the
standard to which this concept applies.
On Jan 2, 2008 4:17 PM, Scott McMurray
On 02/01/2008, Robert Dailey
wrote: They are unrelated types.
But 0 is a compile-time integral constant with value 0, and is therefore implicitly convertible to any null pointer. Why do you think the comparison is any less portable than the initialization?
And the reinterpret_cast is actually less portable, since it assumes that the null pointer is represented by a null bit sequence. The correct way to cast 0 to a null pointer is through static_cast (or boost::implicit_cast). _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 02/01/2008, Robert Dailey
Where in the standard does it state that assigning an integral constant to a pointer type is legal? Perhaps there is some more generic rule in the standard to which this concept applies.
4.10 Pointer conversions [conv.ptr] 1) A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal.

On Wednesday 02 January 2008, Scott McMurray wrote:
On 02/01/2008, Robert Dailey
wrote: Where in the standard does it state that assigning an integral constant to a pointer type is legal? Perhaps there is some more generic rule in the standard to which this concept applies.
4.10 Pointer conversions [conv.ptr] 1) A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Two null pointer values of the same type shall compare equal.
Those interested should check out http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf "A name for the null pointer: nullptr (revision 4)". It's not in the draft standard yet, but will be soon. IIRC it also contains an excellent discussion of the issues involved. -- Dave Steffen - Software Engineer 4 Numerica Corporation (www.numerica.us http://www.numerica.us/ ) 4850 Hahns Peak Drive, Suite 200 Loveland, Colorado 80538 main (970) 461-2000 direct (970) 461-2422 x227 fax (970) 461-2004 Email: dave.steffen@numerica.us This message and any attachments are intended only for the individual or entity to which the message is addressed. This is a private message and may contain privileged information. If you are neither the intended recipient nor the agent responsible for delivering the message to the intended recipient, you are hereby notified that any review, retransmission, dissemination, or taking of any action in reliance upon, the information in this communication is strictly prohibited, and may be unlawful. If you feel you have received this communication in error, please notify me immediately by returning this email to me and deleting it from your computer.

"Robert Dailey"
I can't really agree or disagree, as my information comes from an article I read a long time ago. It stated that the portable way to assign NULL to a pointer is to first cast it to the type of the lvalue.
class foo {}; foo* myfoo = 0;
No, that's guaranteed to work. Either the article you read was wrong, or you misinterpreted it. -Miles -- "Whatever you do will be insignificant, but it is very important that you do it." Mahatma Gandhi

Well thanks guys for everything. I guess I don't need boost at all in this
case after all.
On Jan 2, 2008 8:25 PM, Miles Bader
I can't really agree or disagree, as my information comes from an
"Robert Dailey"
writes: article I read a long time ago. It stated that the portable way to assign NULL to a pointer is to first cast it to the type of the lvalue.
class foo {}; foo* myfoo = 0;
No, that's guaranteed to work. Either the article you read was wrong, or you misinterpreted it.
-Miles
-- "Whatever you do will be insignificant, but it is very important that you do it." Mahatma Gandhi
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Robert Dailey wrote:
They are unrelated types. says who??? if it's your compiler, get one that conforms to the standard
On Jan 2, 2008 4:05 PM, Felipe Magno de Almeida
mailto:felipe.m.almeida@gmail.com> wrote: On Jan 2, 2008 8:02 PM, Robert Dailey
mailto:rcdailey@gmail.com> wrote: > Hi, > > Does boost have some sort of null_ptr class that allows you to do portable > NULL comparisons? For example, the following isn't portable: > > char* foo = 0; > if( foo != 0 ) > { > } Why is this not portable?
[snip]
-- Felipe Magno de Almeida _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org mailto:Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

This is what I came up with, given your code:
static struct nullptr_t
{
template< typename t_type >
operator t_type* () { return 0; }
} nullptr;
One difference is that I made the nullptr object static so it could be
placed in a header without linker conflicts. Your idea is great and simple,
I like it. Thanks a lot.
On Jan 2, 2008 4:06 PM, Kobi Cohen-Arazi
Does boost have some sort of null_ptr class that allows you to do
portable
NULL comparisons?
Would that works for you:
struct nullptr_t { template<class T> operator T* () const { return 0; } }nullptr;
? _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

One difference is that I made the nullptr object static so it could be placed in a header without linker conflicts. Your idea is great and simple, I like it. Thanks a lot.
You are welcome. But it's not my idea. I had the same issue years ago and googled for it. I don't remember who posted it back then. I bet that guy is reading boost-users ;-) Thanks,

On 02/01/2008, Kobi Cohen-Arazi
One difference is that I made the nullptr object static so it could be placed in a header without linker conflicts. Your idea is great and simple, I like it. Thanks a lot.
You are welcome. But it's not my idea. I had the same issue years ago and googled for it. I don't remember who posted it back then. I bet that guy is reading boost-users ;-)
I think this paper mentions it: A name for the null pointer: nullptr (revision 2) www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1601.pdf

Robert Dailey wrote:
Hi,
Does boost have some sort of null_ptr class that allows you to do portable NULL comparisons? For example, the following isn't portable:
char* foo = 0; if( foo != 0 ) { } if (foo) { } should be portable also
But this is portable:
char* foo = reinterpret_cast
( 0 ); if( foo != reinterpret_cast ( 0 ) ) { } You could easily create a null_ptr class with a few global template operators to make this easy, so you could do this: I understand there are enough people who dislike if (foo) and if(!foo) that adding a "null" is going into c+0x I personally like the (foo) and (!foo) notation and think "needing" a "NULL" is pointless given the other holes in the language
char* foo = boost::null_ptr; if( foo != boost::null_ptr ) { }
Is there such a thing? Thanks. ------------------------------------------------------------------------
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Wednesday 02 January 2008, Victor A. Wagner Jr. wrote:
Robert Dailey wrote: [...]
I understand there are enough people who dislike if (foo) and if(!foo) that adding a "null" is going into c+0x I personally like the (foo) and (!foo) notation and think "needing" a "NULL" is pointless given the other holes in the language
It's not just a case of notation, and I don't think that's why "nullptr" is going into the new standard. There are other problems the lack of a null pointer type creates. From the link I posted elsethread ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf ), consider std::string s1( false ); // compiles, calls char* constructor with null std::string s2( true ); // error I'd say that "nullptr" is, rather, closing up one of those holes in the language. (OT: apologies for the long wordy legalese stuff in the sig in my earlier posting. Forgot to clean that up.) -- Dave Steffen - Software Engineer 4 Numerica Corporation (www.numerica.us http://www.numerica.us/ )

On Jan 2, 2008 5:40 PM, Victor A. Wagner Jr.
I personally like the (foo) and (!foo) notation and think "needing" a "NULL" is pointless given the other holes in the language
I agree. Though not "needed", I do think that the nullptr (or whatever is approved by the committee) syntax will improve readability in cases where you need to pass a null pointer value to a function (and you're too lazy to look up the signature). Using 0 in this case does make it slightly easier to confuse the arguments. However I will always use the "boolean style" tests as I find them more intuitive (not to mention more compact syntactically). Jon
participants (8)
-
Dave Steffen
-
Felipe Magno de Almeida
-
Jonathan Franklin
-
Kobi Cohen-Arazi
-
Miles Bader
-
Robert Dailey
-
Scott McMurray
-
Victor A. Wagner Jr.