I need to be able to differentiate between NULL
and 0
in c++.
Is there a way to use something like the ===
operator (such as the identity operator found in JavaScript) to tell the difference between NULL
and 0
in c++?
I need to be able to differentiate between NULL
and 0
in c++.
Is there a way to use something like the ===
operator (such as the identity operator found in JavaScript) to tell the difference between NULL
and 0
in c++?
NULL
is a preprocessor macro, and will be replaced directly with 0 when the preprocessor runs. So in short, no.
Such operator is not necessary in C++, because there is no built-in type that would be capable of storing both these values in a meaningfully distinguishable way. Moreover, NULL
is not required in C++, because you can replace it with zero 0
everywhere a NULL
goes. Bjarne Stroustrup even suggests avoiding NULL
altogether:
In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
There is no difference -- NULL
is required to be defined as an integer constant with the value 0
. The integer type is typically chosen to be the same size as a pointer, but that's not actually necessary. In C it's frequently defined as (void *)0
, but this is not allowed in C++ (in C it's reasonable because a pointer to void supports implicit conversion to any other pointer type--but in C++ that's not allowed, so if NULL
were defined as a pointer to void, you'd have to cast it to get any other pointer type).
When/if you want a null pointer that's distinguishable from 0
, you probably want to use nullptr
. nullptr
can be assigned to a variable of any pointer type, but cannot be assigned to an integer type (e.g., int
, long
, size_t
, etc.)
I think what you're asking is:
If I have a variable
x
, how can I distinguish between
x
contains a numeric0
x
is missing / no value / null pointer
C++ has strongly-typed variables, so it's unusual even to have a variable where both of these are possibilities. But NULL-valued logic is useful in databases, so lets look at a few ways of representing that in C++.
Situation: x == 0
is detected in template code, where the meaning of 0
isn't clear.
Answer: Use a type trait to find out whether x
is a pointer (case #2) or not (case #1).
if (is_pointer(x))
Situation: p
is a C-style NULL-valued logic variable, which is pointer to numeric value.
Answer: Test whether the pointer is null. If not, you can check the pointed-to object.
if (p == NULL) { /* case 2 */ }
else if (*p == 0) { /* case 1 */ }
Situation: v
is a Win32 VARIANT, which is a discriminated union used to implement variables in scripting languages.
Answer: Check the discriminating key.
if (v.vt == VT_EMPTY) { /* case 2a */ }
else if (v.vt == VT_NULL) { /* case 2b */ }
else if (v.vt == VT_I4 && v.lVal == 0) { /* case 1 */ }
else if (v.vt == VT_I2 && v.iVal == 0) { /* case 1 */ }
// and so on
Situation: o
is a C++-ism representation of NULL-valued logic, such as boost::optional
.
Answer: These C++ classes for NULL-valued logic provide a way to detect missing values. A specific example with boost::optional<int>
shows that it's designed to be accessed just like a pointer:
boost::optional<int> o;
if (!o) { /* case 2 */ }
else if (*o == 0) { /* case 1 */ }
In general NULL
and 0
are the same thing in C++ (both are a null pointer).
I'm going to assume you're asking how to get an integral type in C++ which can have both NULL
and 0
values, and to be able to tell the difference.
You can do this with boost::optional
:
boost::optional<int> val;
if(!val)
std::cout << "null" << std::endl;
else
std::cout << "val=" << *val << std::endl;
val = 0;
if(!val)
std::cout << "null" << std::endl;
else
std::cout << "val=" << *val << std::endl;
This should print out null
and val=0
.
Actually it depends on what you are comparing NULL
or 0
with … if you are comparing a integer then NULL
should work as 0
if you are comparing with an address 0
will work as NULL
.
NULL
is a preprocessor macro which will be immediately replaced by 0
before compilation starts.
C++ doesn't have Javascript's operator ===
. The closest thing that comes to that in C++, that I can think of, is a sort pseudo-equivalence relation, which accomplishes the same thing with JS ===
:
if (!(x > y) && !(y > x)) { /* ... */ }