Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ casts

Reply
Thread Tools

C++ casts

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-15-2005
* Jacob:
> It is a common recommendation to use
> "static_cast<SomeType> 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<double>(n) /
> static_cast<double>(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_cast, 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?
 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      08-15-2005
It is a common recommendation to use
"static_cast<SomeType> 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<double>(n) /
static_cast<double>(total);

I find the traditional version a lot more readable
in this case.

Thanks!
 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      08-15-2005

"Jacob" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> It is a common recommendation to use
> "static_cast<SomeType> 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<double>(n) /
> static_cast<double>(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!



 
Reply With Quote
 
pookiebearbottom@yahoo.com
Guest
Posts: n/a
 
      08-15-2005
Is the equivilent of
double fraction = (double) n / total;
really
double fraction = static_cast<double>(n) / total;

double fraction = (double) n / (double) total;

isn't that readable either

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      08-15-2005

Jacob wrote:
> It is a common recommendation to use
> "static_cast<SomeType> 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<double>(n) /
> static_cast<double>(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

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-15-2005
* 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?
 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-15-2005
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!
 
Reply With Quote
 
peter.koch.larsen@gmail.com
Guest
Posts: n/a
 
      08-15-2005

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_cast 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

 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-15-2005
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!

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-15-2005
Greg wrote:

>
> Jacob wrote:
> > It is a common recommendation to use
> > "static_cast<SomeType> 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<double>(n) /
> > static_cast<double>(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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking casts Dan Upton Java 4 12-01-2005 06:20 PM
Web casts in ASP.Net =?Utf-8?B?Q2hyaXMgRGF2b2xp?= ASP .Net 1 10-19-2005 09:45 PM
Epson colour casts on duotones? Bruce Robbins Digital Photography 5 05-25-2004 03:41 AM
Needless casts? Joona I Palaste Java 15 04-25-2004 10:14 PM
Re: = operator should automatically perform appropriate casts cgbusch Java 2 07-08-2003 03:58 PM



Advertisments