Moving to static_cast and reinterpret_cast from old C-style cast

K

Kobe

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_cast ?

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_cast in this case?

Thanks in advance,
K
 
V

Victor Bazarov

Kobe said:
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_cast ?

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_cast in this case?

Only 'reinterpret_cast' will do.

V
 
B

Ben Pope

Kobe said:
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_cast ?

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::size_type count = v.size();
Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
unsigned int DWORD) into pointers.

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

Should I use static_cast or reinterpret_cast in this case?

reinterpret_cast is the tool for the job.

Preferably, don't, of course.

Ben Pope
 
T

Tomás

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_cast ?

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

Then recompile it.

If "static_cast" causes any errors, then change the ones that cause
errors to "reinterpret_cast".

Basically, "reinterpret_cast" can do everything thing that "static_cast"
does, and more.

-Tomás
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top