Description

In C++, 0 is an abused integer. It is used to reflect, well, 0 as a value of type int and it is also used to represent a null pointer... The latter has led to many bugs and confusion over the past 30 years. Put simply, using 0 is and has always been a bad
idea (then there's the NULL macro...). Well, my friends, today, with the release of Visual Studio 2010 Beta 2 and the updated C++ language, compilers and
libraries that come with it, the abuse of 0 comes to an end: Introducing nullptr the rvalue constant that actually
is a null pointer literal.

Who better to dig deep into nullptr (and a few other topics of related interest and importance) than the great and gifted Stephan T. Lavavej? Stephen is a C++ expert and library author who
you've met before a few times on C9. Sit back, relax and learn everything you ever wanted to know about nullptr. Thank you, Stephen, for the awesome lesson!

Another great episode. I'm always glad to see more STL videos. Stephan always has a very clear presentation style. Looking forward to more of these in the future. We need more material from the C++/native side of things.

Regarding the problems with nullptr and C++/CLI. Does this mean that a third-party template library that uses nullptr (rather than __nullptr) in its headers will be unable to be included in any C++/CLI project?

This is a perfectly feasible situation even in interop code, where you have a C++/CLI type wrapping a C++ type which contains a member from this template library.

This would seem to leave the template library little choice but to define a macro to map to either nullptr or __nullptr depending on whether it's a C++/CLI build or not (which it shouldn't care about, since it's a pure C++0x library), which ends up being
worse than the NULL macro!

Short answer is “yes”, I agree that this is perfectly reasonable scenario and, unfortunately, “yes” you may to guard the use of “nullptr” to get it correct in all cases.

Just to be clear, if you write a library and only target native development (with anyone’s compiler including ours) then you use nullptr. If you have a library that is compiled by only Visual C++ and needs to support native and managed, then you use __nullptr
for native and nullptr for managed. The issue is if you do the combination of these (multiple compilers and you want to be able to target both native and managed with Visual C++), then you need a guard.

I am not sure I agree with your last statement however, having “nullptr” (in both native and managed code) solves problems that we cannot solve without them. Unfortunately ECMA C++/CLI and ISO C++ selected the same keyword here but that is life with standard-based
languages. C++/CLI code already exists and, with all other things being equal, we prefer/defer to the "do not breaking existing code" rule.

tivadj: The "managed nullptr" in C++/CLI has no type. The "native nullptr" in C++0x has the type std::nullptr_t. I've been told by Jonathan Caves, the compiler front-end developer who implemented C++0x's nullptr, that this difference is the major stumbling
block to unifying both of them.

Thanks for replying and I understand all of your points. Let me just clarify that when I said 'worse than NULL', I meant bigger and uglier in the code, e.g. MYLIBRARY_NULLPTR or something, as opposed to the semantics which are obviously much better.

However, in general, a C++ library author (such as myself) cannot know in advance whether or not his library will be used in a C++/CLI build or not and, to prevent this being a problem, needs to avoid anything that will cause portability issues. In this
case, avoiding nullptr in favour of 'good old 0'. Obviously this would only be where the semantics don't matter; fortunately, in a template library, you tend to only need to care about the types coming in, rather than the ones you use in the implementation.
But it would've been nice as a self-documentation feature.

Template libraries already have to deal with different compilers (and versions of each compiler) for different reasons; it would've been nice if this otherwise-great new feature didn't have to be avoided or made a special case.