It is a common recommendation to use
"static_cast<So meType> someInstance" instead of the
traditional infamous "(SomeType) someInstance".
Should I use the same practice for simple types,
i.e. instead of:
double fraction = (double) n / total;
should I write this?
double fraction = static_cast<dou ble>(n) /
static_cast<dou ble>(total);
I find the traditional version a lot more readable
in this case.
Thanks! 31 2316
* Jacob: It is a common recommendation to use "static_cast<So meType> someInstance" instead of the traditional infamous "(SomeType) someInstance".
Should I use the same practice for simple types, i.e. instead of:
double fraction = (double) n / total;
should I write this?
double fraction = static_cast<dou ble>(n) / static_cast<dou ble>(total);
I find the traditional version a lot more readable in this case.
There are at least three reasons why it's a Good Idea (TM) to use the C++
named casts also for simple built-in types:
* It helps you and maintainers of your code remember to not use C-style
casts in general, which can help to prevent accidental usage.
* C-style casts can do const_cast and reinterpret_cas t, which you probably
don't want (especially not for pointer values).
* The type definitions may be changed when the code is maintained.
--
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?
"Jacob" <ja***@yahoo.co m> wrote in message
news:3M******** ************@te lenor.com... It is a common recommendation to use "static_cast<So meType> someInstance" instead of the traditional infamous "(SomeType) someInstance".
Should I use the same practice for simple types, i.e. instead of:
double fraction = (double) n / total;
should I write this?
double fraction = static_cast<dou ble>(n) / static_cast<dou ble>(total);
I find the traditional version a lot more readable in this case.
This is TRUE! The traditional version DOES feel a lot natural than the C++
new cast operators.
But this is EXACTLY why C++ cast operators look this way: they are so ugly
looking to discourage you and alert you from using them. And you can always
pick up all casts using a find in a text editor. Thanks!
Is the equivilent of
double fraction = (double) n / total;
really
double fraction = static_cast<dou ble>(n) / total;
double fraction = (double) n / (double) total;
isn't that readable either
Jacob wrote: It is a common recommendation to use "static_cast<So meType> someInstance" instead of the traditional infamous "(SomeType) someInstance".
Should I use the same practice for simple types, i.e. instead of:
double fraction = (double) n / total;
should I write this?
double fraction = static_cast<dou ble>(n) / static_cast<dou ble>(total);
I find the traditional version a lot more readable in this case.
You should avoid casts altogether and write it the C++ way:
double fraction = double(n) / double(total);
assuming that n and total are both a scalar value type convertible to a
double.
Greg
benben wrote: But this is EXACTLY why C++ cast operators look this way: they are so ugly looking to discourage you and alert you from using them. And you can always pick up all casts using a find in a text editor.
So did they really invent this ugly syntax so I
shouldn't use casting, or is this just an urban
myth?
I'd guess there are people from academia telling you
not to cast ever, but no industry scale C++ system
can do without it. And how can I find the fraction of
int 10 to int 35 without casting?
And why do I need a syntax so that _Notepad_ can tell
me where my casts are? First, I wouldn't categorize
code by casts (I don't need a way to find them all),
and a _proper_ editor would tell me (if asked) anyway.
Isn't it so that static_cast (and its cousins) is an
aid for the compiler to to proper type checking and
that neither the compiler nor the runtime environment
can help me when trying to find the fraction of two
integers?
Thanks!
Greg wrote: You should avoid casts altogether and write it the C++ way:
double fraction = double(n) / double(total);
assuming that n and total are both a scalar value type convertible to a double.
I like this. I did search for backing on this syntax
originally but neither S. Meyers nor A. Sutter mentions
it. And my colleges didn't knew about it.
Could you please back it with some links?
Thanks!
* Jacob: Greg wrote:
You should avoid casts altogether and write it the C++ way:
double fraction = double(n) / double(total);
This is not "the C++ way": using C casts is the way of C programmers.
assuming that n and total are both a scalar value type convertible to a double. I like this.
It's C-style casts, expressed in C++ functional notation.
'T(v)' is equivalent to '(T)v'.
I did search for backing on this syntax originally but neither S. Meyers nor A. Sutter mentions it. And my colleges didn't knew about it.
Could you please back it with some links?
§5.2.3/1.
--
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?
Jacob skrev: benben wrote:
But this is EXACTLY why C++ cast operators look this way: they are so ugly looking to discourage you and alert you from using them. And you can always pick up all casts using a find in a text editor. So did they really invent this ugly syntax so I shouldn't use casting, or is this just an urban myth?
I heard that reinterpret_cas t was made purposefully ackward to
discourage its use. I'd guess there are people from academia telling you not to cast ever, but no industry scale C++ system can do without it. And how can I find the fraction of int 10 to int 35 without casting?
I doubt any from academia would tell you never to cast, but casting the
C++ way really is much better. Casting in C++ is much rarer than in C,
however. And why do I need a syntax so that _Notepad_ can tell me where my casts are? First, I wouldn't categorize code by casts (I don't need a way to find them all), and a _proper_ editor would tell me (if asked) anyway.
Well, finding casts is not easy when you use C-syntax. But the primary
raison d'etre for the new casts is that you tell precisely what kind of
cast you're performing. Isn't it so that static_cast (and its cousins) is an aid for the compiler to to proper type checking and that neither the compiler nor the runtime environment can help me when trying to find the fraction of two integers?
There is no aid whatsoever to the compiler. There's a huge aid to the
reader of your code - yourself a half year later or one of your
collegues. A traditional C-style cast does not tell you anything, and
you do cast away const amongst other things. Don't use them. Thanks!
/Peter
Greg wrote: Jacob wrote: It is a common recommendation to use "static_cast<So meType> someInstance" instead of the traditional infamous "(SomeType) someInstance".
Should I use the same practice for simple types, i.e. instead of:
double fraction = (double) n / total;
should I write this?
double fraction = static_cast<dou ble>(n) / static_cast<dou ble>(total);
I find the traditional version a lot more readable in this case.
You should avoid casts altogether and write it the C++ way:
double fraction = double(n) / double(total);
assuming that n and total are both a scalar value type convertible to a double.
I think that's even worse. It's very difficult to search for those, and
it's less obvious at a glance that casting is going on.
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Wagner |
last post by:
I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.
I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or...
|
by: Howard |
last post by:
Hi,
I am maintaining a lot of code that is rife with C-style casts. I've
seen a lot of comments that one should not use C-style casts at all. But
I'm wondering what harm there could be in doing so with the built-in types.
For example, if you have a long which needs to be passed to a function as an
unsigned long, or a char* that needs to be passed as an unsigned char*, or
an unsigned int that needs to be passed as a long, isn't it...
|
by: Ralf |
last post by:
Regarding numerical types, in my view, casts fall in one of two
categories:
1. Casts that change the value of an object
2. Casts that are actually redundant, but get rid of compiler/lint
warnings
As an example, consider this code:
unsigned int ui;
...
|
by: roberts.noah |
last post by:
Where is it in the standard that C style casts are labeled depricated?
I read that on a lot of websites but I can't find it in the standard.
I have BS ISO/IEC 14882:2003 (2nd ed) as published by John Wiley &
Sons. I'm in the middle of a standard argument on this issue and
safety isn't convincing my opponents - deprication might.
|
by: charlie |
last post by:
Hi,
I found an article on informit.com that talks about C++ casts
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=285&rl=1
The strange thing is the author says the following code will *not*
compile:
double d=15.95;
int n= static_cast<int(d);
| |
by: dpbsmith.janissary.2006 |
last post by:
I know C++ mostly from "learning by doing." My main reference is
Stoustrup's book. I was puzzled by something in a colleague's code that
looked like this:
abc=BOOL(def)
I asked him what that was, and he said "it's a cast." I know all about
dynamic_cast and friends, but this was something new to me. To make a
long story short, both of my colleagues seemed to be familiar with
writing a cast as if it were a function, although they don't...
|
by: jacob navia |
last post by:
Hi
I am still writing my tutorial book about C. Here is the section about
casts. I would be interested in your opinions about this. Some people
have definite views about this subject ("never use casts") some others
(like me) are more "liberal" in this subject. I would be interested
in any feedback.
jacob
-------------------------------------------------------------------
|
by: jacob navia |
last post by:
Continuing the discussion about casts, I would like to know
your opinions about the hairy subject of casts as lvalues, i.e.
This will fail under lcc-win32, but MSVC and gcc will
accept it. I know that the standard prescribes the behavior
that lcc-win32 uses, but I left that behavior after a big
discussion about this several years ago. I had modified it,
and some people raised hell.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |