C++11 'nullptr' - does it convert to 'false' implicitly?

Started by
4 comments, last by MaulingMonkey 12 years, 7 months ago
Hi all,

I'm currently starting a new project to just mess about with inheritance and polymorphism (something I've not even looked at for a good 10 years), and I'd like my code to use the new nullptr keyword.

The problem I have is that my compiler doesn't actually support it yet, so as a hack I've, defined it as a const int, like so:
const int nullptr = 0;...for the time being.

This has left me curious as to whether my code will be broken when I do get a compiler that supports nullptr - will nullptr implicity convert to 'false'?

Perhaps it's best with an example. Say I had a pointer to an object, which may contain nullptr or may contain a valid address. Would the following snippet still work?


if (pObject)
{
pObject->someFunction();
// and other stuff
}
else
{
pObject = new Object;
// and other stuff
}


Simlarly, if I had a vector of pointers, some of which are set to nullptr, could I still use this to delete the objects in a class destructor?


for (unsigned int i = 0; i < pObjects.size(); ++i) if (pObject) delete pObject;


Many thanks,
Ronnie
Advertisement
Yes, nullptr evaluates to false. It can pretty much be used as a drop-in replacement for 0 (or NULL if you prefer). I have yet to come across any problems with nullptr that forced me to revert to 0, even when passing it to third-party librarries (using VS 2010)

However, if your taking the plunge into modern C++ you shouldn't be messing with raw pointers at all. Instead learn to love the smart pointer, such as the shared_ptr and the weak_ptr. They will make your life much more enjoyable smile.gif If your compiler doesn't support nullptr, it probably doesn't have builtin smart pointers except std::auto_ptr (which has a bit tricky semantics), but look into Boost.SmartPtr.
I haven't used nullptr myself yet but as I understand it nullptr is only used to distinguish from the integer literal 0. As soon as you assign it to a pointer it will be just a normal null pointer.
I have recently started a new project in VS2010, using much of the C++11 features available there (still waiting for the new for loops and {} initialization, seems slick) and i have yet too come up with a situation where nullptr doesn't work like before. However:



for (unsigned int i = 0; i < pObjects.size(); ++i) if (pObject) delete pObject;


Many thanks,
Ronnie


It is valid C++ (and has always been) to delete null pointers (it does noting), so the if (pObject) is kind of redundant, if that is an actual example from your code. :)
Thank you both for the quick replies. That's great!

As for the Boost library, that's on my 'to do' list of things to look at. At the moment, I'm working my way back through Ivor Horton's Beginning C++ (first edition and positively battered to bits!), and sighing at how much I've forgotten, whilst also applying it to simple projects (and firmly blurring the line between using c and c++ for good measure).

But yes, I've heard so many good things about Boost that I definitely want to learn anout it. As comfortable as I am using pointers, I still drop the ball from time to time.

Thanks again,
Ronnie
Consider using this version of nullptr:

http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr#Solution_and_Sample_Code

The whole point of the new nullptr keyword is to provide a type safe null that won't accidentally be used in an integer expression. Anyone using nullptr with your version of it, expecting said type safety, will be in for a horrible surprise.

This topic is closed to new replies.

Advertisement