I see some code use static_cast<som e_pointer_type> (0) instead of NULL
to describe null pointer. I'm wondering what is the pros and cons of
each way. Is there any reason why we should one verses the other. 19 3727 Pe*******@gmail .com wrote: I see some code use static_cast<som e_pointer_type> (0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the other.
What code does it the complex way? Could you post a sample?
NULL is magic, and should generally always appear as the constant "NULL".
Maybe you saw code that upgraded from C, which used (void*)0.
NULL is magic because a constant 0, in C++, always freely converts to any
pointer type, just as typesafely as if you had put an elaborate cast on it.
Don't.
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Phlip wrote: Pe*******@gmail .com wrote:
I see some code use static_cast<som e_pointer_type> (0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the other. What code does it the complex way? Could you post a sample?
template <class T>
class Nil {
public:
operator T* () { return static_cast<T*> (0); }
};
template <class T>
void Delete(T*& x) {
delete x;
x = Nil<T>();
}
The above is the code fragment.
In general, NULL is preferred, right? NULL is magic, and should generally always appear as the constant "NULL". Maybe you saw code that upgraded from C, which used (void*)0.
NULL is magic because a constant 0, in C++, always freely converts to any pointer type, just as typesafely as if you had put an elaborate cast on it. Don't.
-- Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!! Pe*******@gmail .com wrote: Phlip wrote: Pe*******@gmail .com wrote:
I see some code use static_cast<som e_pointer_type> (0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the other.
What code does it the complex way? Could you post a sample?
template <class T> class Nil { public: operator T* () { return static_cast<T*> (0); }
That cast is invalid and, luckily, unnecissary.
You can't static_cast between unrelated types. An int and a T* are
totally unrelated. Luckily enough, 0 is magic in that it can be any
pointer as well as an integral. So, that is just calling a static cast
from T* to T*...if it wasn't the code would not compile.
What the original coder probably intended was a reinterpret cast.
However, since the static cast worked it shows that it is not necissary
to perform any casting at all.
* Noah Roberts wrote, on 31/03/2006 22:53: Pe*******@gmail .com wrote: template <class T> class Nil { public: operator T* () { return static_cast<T*> (0); } That cast is invalid
No.
and, luckily, unnecissary.
Yes.
[snip] What the original coder probably intended was a reinterpret cast.
No, that would be invalid. ;-)
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Noah Roberts wrote: template <class T> class Nil { public: operator T* () { return static_cast<T*> (0); }
That cast is invalid and, luckily, unnecissary.
I want to know why the Nil class is there. If its only purpose is this line
delete x;
x = Nil<T>();
then it is an excessive and unnecessary way to write x = NULL.
Someone may have Template Fever here. ;-)
--
Phlip http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Phlip wrote: Someone may have Template Fever here. ;-)
Unfortunately, a far too common ailment.
--
Pete Becker
Roundhouse Consulting, Ltd. Pe*******@gmail .com wrote: I see some code use static_cast<som e_pointer_type> (0) instead of NULL to describe null pointer. I'm wondering what is the pros and cons of each way. Is there any reason why we should one verses the other.
Consider:
MyStream& operator<<(MySt ream&, int);
MyStream& operator<<(MySt ream&, const char*);
MyStream m;
m << NULL;
m << static_cast<cha r*>(0);
HTH
Michiel Salters
Alf P. Steinbach wrote: * Noah Roberts wrote, on 31/03/2006 22:53: Pe*******@gmail .com wrote: template <class T> class Nil { public: operator T* () { return static_cast<T*> (0); }
That cast is invalid
No.
and, luckily, unnecissary.
Yes.
[snip] What the original coder probably intended was a reinterpret cast.
No, that would be invalid. ;-)
Interesting. How do you figure? Since 0 is an int (except when used
as a pointer, which such added behavior is what renders any cast moot)
I can't think of any static_cast that is valid. In fact g++ pukes when
you try to static_cast but allows the reinterpret_cas t through just
fine.
int main()
{
int x = 0;
int *r = reinterpret_cas t<int*>(x);
int *p = static_cast<int *>(x);
return 0;
}
g++ stat.cpp
stat.cpp: In function `int main()':
stat.cpp:5: error: invalid static_cast from type `int' to type `int*'
g++ seems to think it is an invalid cast. So do I.
The OP wrote: > operator T* () { return static_cast<T*> (0); }
Noah Roberts wrote:
That cast is invalid
Alf P. Steinbach wrote:
No.
Noah Roberts:
What the original coder probably intended was a reinterpret cast.
Alf Steinbach:
No, that would be invalid. ;-)
Noah Roberts:
Interesting. How do you figure? Since 0 is an int (except when used as a pointer, which such added behavior is what renders any cast moot)
You're starting from a flawed premise - or rather, your exception
swallows the rule.
I can't think of any static_cast that is valid. In fact g++ pukes when you try to static_cast but allows the reinterpret_cas t through just fine.
int main() { int x = 0; int *r = reinterpret_cas t<int*>(x); int *p = static_cast<int *>(x);
return 0; }
Yes, but the following compiles fine:
int main()
{
int *r = reinterpret_cas t<int*>(0);
int *p = static_cast<int *>(0);
}
What started out this subthread was your assertion that
static_cast<T*> (0) is an invalid cast. Alf Steinbach said it was
valid. He's right. In response you said that static_cast<T*> (i) was
an invalid cast where i==0. That's true, but different from your
original assertion.
Best regards,
Tom This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jiong Feng |
last post by:
Hi,
Here is my problem:
I created a try.htm on my server, which contains a link to the default.aspx page.
if I use http://localhost/try.htm, and click the link, then in default.aspx, I could get the correct HttpContext.Current.Request.UrlReferrer.
but if I use https://localhost/try.htm (my server could use https), and click the link, then in default.aspx, the correct HttpContext.Current.Request.UrlReferrer is null.
Is that by design...
|
by: Pesso |
last post by:
I'm having a difficulty compairing null to a class object whose "equal"
operator is overridden.. Consider the following:
class Foo
{
// ...
public static bool operator==(Foo f1, Foo f2)
{ return (f1.n == f2.n); }
Now the following throws:
|
by: Joe Van Dyk |
last post by:
When you delete a pointer, you should set it to NULL, right?
Joe
|
by: Richard Coltrane |
last post by:
Hi there,
Im stepping into C# from VB.net. In all the examples ive seen about raising
events the following construct is used:
if (myevent != null)
myevent(this,args);
Whats the purpose of the test for null? Is that testing to see if the
underlying delegate is null? If so when would it be?
|
by: Debbiedo |
last post by:
I searched the groups and tried several approaches but still cannot
find a solution.
I have a table that has several hundred fields that may or may not
need to be displayed in a report, depending on whether they are NULL
or not. (Due to limitations of out software, the table needs to be
designed this way)
The data needs to be displayed like this:
| |
by: shankwheat |
last post by:
I'm having trouble adding two values together when one of them has a
null value
// These two values come from a database
CEOBonusValue = 550000
CEONonEqIncentCompHidden == null
This should alert to true but doesn't
|
by: cmartin1986 |
last post by:
I have written a sql query and I need it to return 0 when it doesn't find any matches to my criteria. I have tried adding iif statements, tried sum, and just Count, all of these methods work fine to return the values when it finds matches, but i need it also to return a 0 when there are no matches. Here is what I got.
SELECT "CAL Recieved" as Tags, .,sum(iif(.,1,0)) AS
FROM
GROUP BY .
HAVING (((.)=(Date()-1)));
UNION ALL
SELECT...
|
by: Joe |
last post by:
I'm binding to a column in a TemplateField. In some cases the join I have
returns a null for an int field. I would like to specify a default value
somehow so I don't end up with a null exception.
I'm binding the CurrentRating of the Rating control so I'm doing
Convert.ToInt32(Eval('MyField')). This of course throws an exception if the
value being returned is null.
Is there another means of binging to that property where either a null...
|
by: Francois Grieu |
last post by:
When running the following code under MinGW, I get
realloc(p,0) returned NULL
Is that a non-conformance?
TIA,
Francois Grieu
#include <stdio.h>
#include <stdlib.h>
|
by: Heikki Toivonen |
last post by:
I was debugging M2Crypto function written in C which changed behavior
between Python 2.6 and earlier Python versions. In an error condition
the function was supposed to raise exception type A, but with 2.6 it
raised type B, and further, there was no string value for the exception.
I tracked this down to the C code incorrectly returning Py_None when it
should have returned NULL. Changing the C code to return NULL made it
behave correctly...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |