static_cast vs reinterpert_cast

R

Rolf Magnus

Frederick said:
Rolf Magnus posted:




There's an implicit conversion from int to size_t, so no cast is required.

Well, it's there, no matter if it's required or not.
When dealing with intrinsic types, the following are exactly equivalent:

short(5)

(short)5

Yes. Both are casts.
I myself prefer the latter, for two reasons:

(1) You can have types which consist of more than one word, e.g.
"unsigned short".

That's a good point.
(2) It resembles a cast.

Well, it is a cast, just like the first one.
I dislike the former because:

(1) It looks like the construction of a user-defined class type.

That's one reason why I like it. It uses built-in types more consistently
with classes by using the same syntax.
Btw: What do you do in a template, where the type might be either one?
(2) It doesn't resemble a cast at all.

Well, if you are used to the latter, because you know it from C, then you
might think like that. But basically, none of the two looks more "castish"
than the other.
(3) You can't use types which consist of more than one word.

Yes, that's a restriction that will hopefully be fixed some time.
One thing I like about it is that the value is in parenthesis, so in a
longer exression, it's easier to see which part of it is cast.
 
R

Richard Herring

Frederick Gotham said:
Richard Herring posted:



Yes but this has re-arranged the natural order of the loop.

I like my "for" loops to have four phases:

(1) The "initialisation stuff" is executed first of all.

(2) The condition is tested.

(3) The body is executed.

(4) The "prepare for next iteration" stuff is executed.


Your example mixes (2) and (4), which I don't like.
It puts (4) before (3), that's all. It's just another idiom, and a very
clear one once you're used to it. The only numbers that appear in it are
0 and len, exactly the same as for the forward loop. Other solutions all
require an off-by-one representation of the index and either mix signed
and unsigned, as with the cast above, or do something even more
contorted.
 
F

Frederick Gotham

Richard Herring posted:

It puts (4) before (3), that's all. It's just another idiom, and a very
clear one once you're used to it. The only numbers that appear in it are
0 and len, exactly the same as for the forward loop. Other solutions all
require an off-by-one representation of the index and either mix signed
and unsigned, as with the cast above, or do something even more
contorted.


Let's put it down to a difference in taste : ).
 
R

Ron Natalie

Jim said:
Actually, not at all. size_t(-1) tells me exactly what is going on. This
is a size_t variable that is being initialized to -1. There is no ambiguity
at all. It is not techinically a cast, I don't think.
It's is exactly the same as the c-style cast.
The definition of the "explicit type conversion (functional notation)"
says:

If the expression list is a single expression, the type conversion
expression is equivalent (in definedness, and if defined in meaning) to
the corresponding cast expression (5.4).
 
R

Ron Natalie

Frederick said:
When dealing with intrinsic types, the following are exactly equivalent:

short(5)

(short)5

They are exactly equivelent no matter WHAT type is being used.
 

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
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top