cast constant vs. construct temporary

A

Adam

Is there a difference between the expressions "size_t(-1)" and "(size_t)-1"?

In context, say:
void Some_Function(size_t);
Some_Function(size_t(-1)); //pass a flag value to Some_Function
 
A

Alf P. Steinbach

* Adam:
Is there a difference between the expressions "size_t(-1)" and
"(size_t)-1"?

The former is defined to mean the same as the latter, as an isolated
expression.
 
T

Tomás

Alf P. Steinbach posted:
* Adam:

The former is defined to mean the same as the latter, as an isolated
expression.

I was very surprised to hear that!

I would've thought that the former was a static_cast (which could also
call explicit contructors), and that the latter was a reinterpret_cast.

But to my horror, the following compiled:

int main()
{
typedef char* PC;

PC p;

p = PC(43);
}

I'll think twice before using the "function" form in future.

I think most people shy away from using the new casts (static_cast,
dynamic_cast, reinterpret_cast, const_cast) because they're just too many
characters to type (I often find myself thinking "aw crap I've to write
that entire word out"). Lately I've been using typedef's for unwieldy
names like "unsigned char", peferring "uchar" in its place. I might start
using the likes of: scast, dcast, rcast, ccast. Would #define be the best
way to do this?

#define scast static_cast
#define rcast reinterpret_cast
#define dcast dynamic_cast
#define ccast const_cast


-Tomás
 
A

Alf P. Steinbach

* Tomás:
I might start
using the likes of: scast, dcast, rcast, ccast. Would #define be the best
way to do this?

#define scast static_cast
#define rcast reinterpret_cast
#define dcast dynamic_cast
#define ccast const_cast

Try something like

template< typename D, typename S >
inline D scast( S x ) { return static_cast<D>( x ); }

Of course passing by value is ungood for large types.
 
T

Tomás

Alf P. Steinbach posted:
* Tomás:

Try something like

template< typename D, typename S >
inline D scast( S x ) { return static_cast<D>( x ); }


I think #define is the only viable option because the above code won't
work for the following:

int main()
{
Derived derived;

Base &base = scast<Base&>( derived );
}

Having considered that the "function form cast" is identical to the "C-
style cast", I finally recognise the merit in having an "implicit_cast".
For instance:

template<class A, class B>
A implicit_cast( B b )
{
return b;
}

int main()
{
int k;

void *vp = &k;

char *p = implicit_cast<A>( vp ); /* Won't compile! */

/* However, it WOULD compile with static_cast */
}


However, we still have the problem of passing by value/reference, and
also returning by value/reference.

Unfortunately C++ still hasn't found a way of completely replacing macros
(unless there's some metaprogramming template technique that I'm not
aware of...)


-Tomás
 
N

Noah Roberts

Tomás said:
I think most people shy away from using the new casts (static_cast,
dynamic_cast, reinterpret_cast, const_cast) because they're just too many
characters to type (I often find myself thinking "aw crap I've to write
that entire word out").

I hear people complain about that a lot and I don't understand how
people can be so lazy. You should always have descriptive instead of
terse names in your code anyway; it aids in readability. Casts are no
differenent.

Besides, most programming editors have some sort of name completion
that makes how long a name is a moot point 99% of the time. My editor
for instance will pick up "reinterpret_cast" after 3 characters and
then I just hit the tab key.

I find it really sad when people use this argument against using the
obviously stronger and more meaningful casts and even worse when people
buy into it.

Lately I've been using typedef's for unwieldy
names like "unsigned char", peferring "uchar" in its place. I might start
using the likes of: scast, dcast, rcast, ccast. Would #define be the best
way to do this?

#define scast static_cast
#define rcast reinterpret_cast
#define dcast dynamic_cast
#define ccast const_cast

Sacrifice readibily to avoid typing 11 extra characters at most....sad.
 
J

Jack Saalweachter

Noah said:
Tomás wrote:




I hear people complain about that a lot and I don't understand how
people can be so lazy. You should always have descriptive instead of
terse names in your code anyway; it aids in readability. Casts are no
differenent.

Besides, most programming editors have some sort of name completion
that makes how long a name is a moot point 99% of the time. My editor
for instance will pick up "reinterpret_cast" after 3 characters and
then I just hit the tab key.

I find it really sad when people use this argument against using the
obviously stronger and more meaningful casts and even worse when people
buy into it.
The real question, in my mind, is "Why are you spending so much time
writing code with casts?"
 
N

Noah Roberts

Jack said:
The real question, in my mind, is "Why are you spending so much time
writing code with casts?"

Well, if you work in certain realms it is unavoidable. For instance,
if you work with win32 (not MFC) then you are likely to have a lot of
reinterpret_casts in your code. Many times when you are working with a
C based API this is the case. Also, doing double based vector drawing
will eventually require static_casts to integer values when going to
any pixel based drawing area, such as a screen or printer. You work
with doubles because they are more accurate, but the device itself
won't support them. There are other cases when this is how things are
done.

I agree though, casting is indicative of poor design choices and you
really shouldn't need to do it often enough to warrant needing
shortcuts. Really though, code without casts only works in theory; in
practice they find their way in. Usually you can limit them to a
smaller area and provide an interface that doesn't require
casting...but still...they get in.
 
T

Tomás

Noah Roberts posted:
Sacrifice readibily to avoid typing 11 extra characters at
most....sad.


It's the sentiment behind it more than anything. Who would want to write:

signed integer

rather than:

int


-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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top