I'm just a little bit confused.
When should I use nullptr?
I've read on some sites that it should always be used, but I can't set nullptr for a non-pointer for example:
int myVar = nullptr; // Not a pointer ofcourse
Should I always use NULL non-pointers and nullptr for pointers?
Thanks to any help! I'm very new to c++ 11 (and c++ overall).
Always use
nullptrwhen initializing pointers to a null pointer value, that's what it is meant for according to draft n3485.Now onto the use of
NULL.According to the same draft it shall be defined as follows.
and null pointer constant as follows.
That is, it is implementation-defined behavior whether
NULLis defined as an integer prvalue evaluating to zero, or a prvalue of typestd::nullptr_t. If the given implementation of the standard library chooses the former, thenNULLcan be assigned to integer types and it's guaranteed it will be set to zero, but if the later is chosen, then the compiler is allowed to issue an error and declare the program ill-formed.In other words, although conditionally valid [read IB], initializing an integer using
NULLis most probably a bad idea, just use 0 instead.On the other hand, according to the above
NULLis guaranteed to initialize pointers to a null pointer value, much likenullptrdoes, but whileNULLis a macro, what accompanies several caveats,nullptris prvalue of a specific type, for which type checking and conversion rules apply. That's mostly whynullptrshould be prefered.