Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   static_cast<float>(a) versus float(a) (http://www.velocityreviews.com/forums/t280659-static_cast-float-a-versus-float-a.html)

Jim West 01-15-2004 07:06 PM

static_cast<float>(a) versus float(a)
 
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.

Ron Natalie 01-15-2004 08:01 PM

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

"Jim West" <jwest@jwest.ecen.okstate.edu> wrote in message news:slrnc0dpl2.msb.jwest@emag.ecen.okstate.edu...

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


Andrey Tarasevich 01-15-2004 10:21 PM

Re: static_cast<float>(a) versus float(a)
 
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


PETER_ 01-16-2004 12:45 AM

Re: static_cast<float>(a) versus float(a)
 
Jim West <jwest@jwest.ecen.okstate.edu> wrote in message news:<slrnc0dpl2.msb.jwest@emag.ecen.okstate.edu>. ..
> 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.

Jeff Schwab 01-16-2004 12:36 PM

Re: static_cast<float>(a) versus float(a)
 
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. :)



All times are GMT. The time now is 12:14 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.