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

J

Jim West

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

Ron Natalie

Jim West said:
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.
 
A

Andrey Tarasevich

Jim said:
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.
 
P

PETER_

Jim West said:
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.
 
J

Jeff Schwab

Jim said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top