Posted By: Charles | Oct 19th, 2009 @ 9:39 AM | 33,026 Views | 10 Comments

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!

Enjoy!

VC Team Blog: http://blogs.msdn.com/vcblog/default.aspx

Rating:
3
0

Wow, I couldn't have imagined how using NULL could cause so much weirdness

Regarding conflicting types nullptr_t in native and unmanaged sides.

 

In .NET you can't write typeof(null) because null has no type. How it is turned out for nullptr in C++/CLI to have a type?

Could you elaborate?

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.

 

Fuz
Fuz

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!

Hello Fuz

 

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.

 

Thanks

Damien

Hello Fuz

 

PS: Stephan post a reply on the VC Blog to this too: http://blogs.msdn.com/vcblog/archive/2009/10/22/visual-studio-2010-beta-2-is-now-available-for-download.aspx#9913727.                                                                                                             

 

Thanks

Damien

STL
STL

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.

Microsoft Communities