By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,495 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,495 IT Pros & Developers. It's quick & easy.

cast constant vs. construct temporary

P: n/a
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
May 24 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* 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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 24 '06 #2

P: n/a
Alf P. Steinbach posted:
* 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.


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
May 24 '06 #3

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 24 '06 #4

P: n/a
Alf P. Steinbach posted:
* 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 ); }

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
May 24 '06 #5

P: n/a

Tomás wrote:
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.

May 24 '06 #6

P: n/a
Noah Roberts wrote:
Tomás wrote:

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.

The real question, in my mind, is "Why are you spending so much time
writing code with casts?"
May 24 '06 #7

P: n/a

Jack Saalweachter wrote:
Noah Roberts wrote:
Tomás wrote:

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.

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.

May 24 '06 #8

P: n/a
Noah Roberts posted:
#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.

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

signed integer

rather than:

int
-Tomás
May 24 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.