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.
===
operator, in Javascript. He's asking what the equivalent is in C++.
May 10, 2013 at 18:37
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.
NULL
and 0
: all integral types. NULL
simply is 0
. There is of course no type which can store both 0 and a null pointer, but that's a different story.
May 10, 2013 at 18:34
vt == VT_EMPTY
or vt == VT_NULL
) and boost::variant
and boost::optional
.
May 10, 2013 at 18:41
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.)
0
or 0u
or 0uL
or 0uLL
-- it specifically is required to have an integral type.
May 10, 2013 at 18:30
nullptr
isn't distinguishable from a pointer set with 0
. The essential difference is that nullptr
has no implicit conversion to any numeric type.
May 10, 2013 at 18:35
NULL
can be defined either as an integer constant expression with the value 0 (such as 0
) or as such an expression cast to void*
(such as ((void*)0)
). Parentheses are added as needed to ensure that it's a primary expression and avoid operator precedence problems. C++ doesn't permit the ((void*)0)
form.
May 10, 2013 at 20:42
NULL
to be defined as ((void*)0)
is that C++ doesn't allow void*
to be implicitly converted to other pointer types like in C.
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 */ }
nullptr
is completely irrelevant to this question. A null pointer value is a null pointer value, whether the source code used 0
or nullptr
.
May 11, 2013 at 3:45
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)) { /* ... */ }
int*
andint
are two entirely different things. You'd have to cast in order to get these to compare, which means you're going out of your way to make this comparison ambiguous.nullptr
instead ofNULL
, problem solved.NULL
and0
" in C++ simply becuase in general case they are exactly the same. If you are talking about something else, you have to provide more deatils about what you are trying to do.