473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.
Jul 22 '05 #1
4 4994

"Jim West" <jw***@jwest.ecen.okstate.edu> wrote in message news:sl******************@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.

Jul 22 '05 #2
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

Jul 22 '05 #3
Jim West <jw***@jwest.ecen.okstate.edu> wrote in message news:<sl******************@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.
Jul 22 '05 #4
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. :)

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: GuineaPig | last post by:
Hello, I'm (very) new to c++ and I'm having trouble understanding why this doesn't work. Here's some testcode: #include <iostream.h> int main() { float test;
11
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
9
by: alopatenko | last post by:
I have a template class template <Class W> class WS At some point I have to use a STL list WS<W>objects so, I define #include <list>
3
by: Nate Barney | last post by:
I have: // base class for Vector and Matrix template <unsigned N,typename Value=float> class NonScalar { public: Value *ptr() { return e; } const Value *ptr() const { return e; }
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
9
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.