Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static_cast<float>(a) versus float(a)

Reply
Thread Tools

static_cast<float>(a) versus float(a)

 
 
Jim West
Guest
Posts: n/a
 
      01-15-2004
Both

int a;
std::complex<float> b;
std::complex<float> c = static_cast<float>(a)*b;

and

int a;
std::complex<float> b;
std::complex<float> c = float(a)*b;

compile to exactly the same assembler on two different compilers.
However, I always seem to miss the subtleties of the language. Is
one version to be preferred over the other? I tend to use the latter
since it is more compact, which based on my track record is probably
the wrong way to go.

Stylistic suggestions greatly appreciated.
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-15-2004

"Jim West" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...

> compile to exactly the same assembler on two different compilers.
> However, I always seem to miss the subtleties of the language. Is
> one version to be preferred over the other? I tend to use the latter
> since it is more compact, which based on my track record is probably
> the wrong way to go.
>

They both are required to do the same thing. The C cast prefers the
static_cast behavior over reinterpret_cast. Of course unless you're
dealing with pointers or references, the reinterpret_cast won't ever fire
so it's pretty safe.

 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      01-15-2004
Jim West wrote:
> Both
>
> int a;
> std::complex<float> b;
> std::complex<float> c = static_cast<float>(a)*b;
>
> and
>
> int a;
> std::complex<float> b;
> std::complex<float> c = float(a)*b;
>
> compile to exactly the same assembler on two different compilers.
> However, I always seem to miss the subtleties of the language. Is
> one version to be preferred over the other?


Both versions do the same thing. One should be preffered just for the
sake of consistency, but which one should that be is purely a matter of
personal choice.

> I tend to use the latter
> since it is more compact, which based on my track record is probably
> the wrong way to go.


No, in this context there's nothing wrong with it. I, for example,
prefer using "old" C-style casts for arithmetical conversions.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
PETER_
Guest
Posts: n/a
 
      01-16-2004
Jim West <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> Both
>
> int a;
> std::complex<float> b;
> std::complex<float> c = static_cast<float>(a)*b;
>
> and
>
> int a;
> std::complex<float> b;
> std::complex<float> c = float(a)*b;
>
> compile to exactly the same assembler on two different compilers.
> However, I always seem to miss the subtleties of the language. Is
> one version to be preferred over the other? I tend to use the latter
> since it is more compact, which based on my track record is probably
> the wrong way to go.
>
> Stylistic suggestions greatly appreciated.


If I recall correctly using the c-style cast instead of the c++ style
cast can
allow for a reinterpret_cast<TYPETARGET>(TYPESOURCE) to occur when the
correct
conversion operators or constructors are unavailable. For built-in
types this may not be true.

However, your code snippet seems to indicate using C++ conversion
features and therefore your codes intent might be to require explicit
conversion rather than compiler defaulted conversion. If instead of
"int a" it was "MyClass a" the static_cast would be more specific
that you were expecting a conversion operation and not a bitwise
interpretation of the "a" bits.
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      01-16-2004
Jim West wrote:
> Both
>
> int a;
> std::complex<float> b;
> std::complex<float> c = static_cast<float>(a)*b;
>
> and
>
> int a;
> std::complex<float> b;
> std::complex<float> c = float(a)*b;
>
> compile to exactly the same assembler on two different compilers.
> However, I always seem to miss the subtleties of the language. Is
> one version to be preferred over the other? I tend to use the latter
> since it is more compact, which based on my track record is probably
> the wrong way to go.
>
> Stylistic suggestions greatly appreciated.


I tend to prefer the first, so I can grep for casts.

"(float)a" of course remains hideous.

 
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
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
Certification - MCP versus MCSA versus MCSE =?Utf-8?B?Q2FuYWRhX0d1eU0=?= Microsoft Certification 1 08-24-2006 06:33 PM
script versus code versus ? Russ ASP .Net 1 06-10-2004 03:06 AM
HTML Client Control versus. HTML Server Control versus. Web Server Control Matthew Louden ASP .Net 1 10-11-2003 07:09 PM



Advertisments