473,804 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Moving to static_cast and reinterpret_cas t from old C-style cast

Hi,

if I need to convert a size_t to an int, in "older" C++ I'd write the
following code (using C-like "casting"):

<CODE>
std::vector<... > v;
int count = (int) v.size();
// v.size() returns size_t
</CODE>

Now that modern C++ has different cast tools, how the above code should
be changed? Should I use static_cast or reinterpret_cas t ?

e.g.

int count = static_cast<int >(v.size());

Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
unsigned int DWORD) into pointers.

e.g.

<CODE>
// Old style C casting
DWORD address;
// address = ...
AClass * myObject = (AClass *)address;
</CODE>

Should I use static_cast or reinterpret_cas t in this case?

Thanks in advance,
K
Feb 15 '06 #1
3 6003
Kobe wrote:
if I need to convert a size_t to an int, in "older" C++ I'd write the
following code (using C-like "casting"):

<CODE>
std::vector<... > v;
int count = (int) v.size();
// v.size() returns size_t
</CODE>

Now that modern C++ has different cast tools, how the above code should
be changed? Should I use static_cast or reinterpret_cas t ?

e.g.

int count = static_cast<int >(v.size());
That should suffice. Beware, though, that if the value of 'v.size()'
cannot be represented in an int, the behaviour is implementation-
defined, and you may end up with a negative 'count'. Do you really need
'count' to be 'int'?
Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
unsigned int DWORD) into pointers.
Are you sure they will fit?
e.g.

<CODE>
// Old style C casting
DWORD address;
// address = ...
AClass * myObject = (AClass *)address;
</CODE>

Should I use static_cast or reinterpret_cas t in this case?


Only 'reinterpret_ca st' will do.

V
--
Please remove capital As from my address when replying by mail
Feb 15 '06 #2
Kobe wrote:
Hi,

if I need to convert a size_t to an int, in "older" C++ I'd write the
following code (using C-like "casting"):

<CODE>
std::vector<... > v;
int count = (int) v.size();
// v.size() returns size_t
</CODE>

Now that modern C++ has different cast tools, how the above code should
be changed? Should I use static_cast or reinterpret_cas t ?

e.g.

int count = static_cast<int >(v.size());
That should work with the obvious limitation.

What's wring with:
typedef std::vector<... > myContainer;
myContainer v;
myContainer::si ze_type count = v.size();
Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
unsigned int DWORD) into pointers.
<shudder/>
<CODE>
// Old style C casting
DWORD address;
// address = ...
AClass * myObject = (AClass *)address;
</CODE>

Should I use static_cast or reinterpret_cas t in this case?


reinterpret_cas t is the tool for the job.

Preferably, don't, of course.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 15 '06 #3
Kobe posted:
Hi,

if I need to convert a size_t to an int, in "older" C++ I'd write the
following code (using C-like "casting"):

<CODE>
std::vector<... > v;
int count = (int) v.size();
// v.size() returns size_t
</CODE>

Now that modern C++ has different cast tools, how the above code should
be changed? Should I use static_cast or reinterpret_cas t ?

e.g.

int count = static_cast<int >(v.size());

Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
unsigned int DWORD) into pointers.

e.g.

<CODE>
// Old style C casting
DWORD address;
// address = ...
AClass * myObject = (AClass *)address;
</CODE>

Should I use static_cast or reinterpret_cas t in this case?

Thanks in advance,
K


Here's what I'd do:

Where ever you have a C-style cast, replace it with "static_cas t".

Then recompile it.

If "static_cas t" causes any errors, then change the ones that cause
errors to "reinterpret_ca st".

Basically, "reinterpret_ca st" can do everything thing that "static_cas t"
does, and more.

-Tomás
Feb 15 '06 #4

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

Similar topics

1
2403
by: Marc Schellens | last post by:
Can somebody tell me when exactly a static_cast is invalid, and when not? thanks, marc
17
12867
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. To get around this limitation on double, I'd like to keep the bits of the double the *same* but change its interpretation to long. I can use "&" on longs. I tried to use reinterpret_cast for this purpose, but it returned zero every time. double...
11
5162
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 one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
2
1962
by: Amit | last post by:
Greetings. I am having some problem while using a cast operation(static_cast and/or dynamic_cast) between base and derived objects when passing to functions. what I have is something like this.. template <class T> class Node{ protected: T e; Node* left;
24
4772
by: Rahul | last post by:
Hi, I have a class A : public B {...member functions......data members}; and am doing the following A *p=new A(); void *p=static_cast<void *>(p); factory_instance->process(p);
9
8304
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 `>' token in that line
2
2963
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
1
7143
by: =?utf-8?B?5Zyw55CD5Y+R5Yqo5py6?= | last post by:
In C++ we can not overload the explicit typecast operator like static_cast, dynamic_cast, const_cast and reinterpret_cast. The only we can do is the implicit typecast operator like operator MyClass::MyDistType(). I agree there are maybe no meaning to overload dynamic_cast/const_cast/ reinterpret_cast, except the static_cast. If we enable the overloading of static_cast, we could be able to force the user doing type casting explicitly.
2
2683
by: ciccio | last post by:
Hi, I was wondering what the main reason is why reinterpret_cast fails to work as expected when using optimizations. Here is the simple example code which fail to give the correct result when optimizing. #include <iostream> int main() { long D1 = 0xbcfbc4f0d9b65179; long D2 = 0xbcfbc4f0d9b65042;
2
4310
by: zeeshan708 | last post by:
what is the difference between a reinterpret_cast and static_cast,,both do casting at compile time so whats the difference????
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10097
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.