Quick cast style question

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

?
 
L

Leor Zolman

If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

Personally, I'd use the reinterpret_cast, since the compiler will diagnose
attempts to use it in a situation where one of the other new-style casts
would be the right one. So the former is saying "do the cast if in fact we
are truly requiring reinterpret, and the others wouldn't do, else tell me
I've screwed up", while the latter says "just shut up and do it."
-leor
 
K

Karthik

Christopher said:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

?
Somehow I am always sceptic with the usage of reinterpret_cast here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_cast is dangerous and would probably tie you to a specific
implementation.
 
B

bartek

If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

C-style casts were provided for compatibility with old code. In C++ you
should only use the 'new'-style casts, as they show your intents clear in
the code. 'reinterpret_cast' is there to show that something drastic is
going on.

Code editors should highlight it in bloody red... or even flashing bloody
red.

Cheers.
 
B

bartek

Somehow I am always sceptic with the usage of reinterpret_cast here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_cast is dangerous and would probably tie you to a specific
implementation.

'reinterpret_cast' is the only option in that situation. The compiler
would refuse to compile the above code when 'static_cast' was used.
'dynamic_cast', on the other hand, only works for casting
pointers/references within an inheritance hierarchy.

Cheers.
 
L

Leor Zolman

Somehow I am always sceptic with the usage of reinterpret_cast here.
I would go for static_cast or dynamic_cast , as necessary. Using
reinterpret_cast is dangerous and would probably tie you to a specific
implementation.

You may want to try the different forms and examine the results. In fact,
reinterpret_cast was the correct choice here (of the different casts. I
can't speak to the appropriateness here of using a cast, versus not using
one, in the first place...)
-leor
 
J

jeffc

Christopher Benson-Manica said:
If you had an unsigned int that needed to be cast to a const myClass*,
would you use

const myClass* a=reinterpret_cast<const myClass*>(my_val);

or

const myClass* a=(const myClass*)myVal;

Prefer the former - it was added to C++ as an improvement over the latter.
 
C

Christopher Benson-Manica

Leor Zolman said:
You may want to try the different forms and examine the results. In fact,
reinterpret_cast was the correct choice here (of the different casts. I
can't speak to the appropriateness here of using a cast, versus not using
one, in the first place...)

Well, the design decision that makes a cast necessary in the first
place is probably of debatable merit. Given that, the cast is pretty
much required...
 
J

Jake Montgomery

bartek said:
C-style casts were provided for compatibility with old code. In C++ you
should only use the 'new'-style casts, as they show your intents clear in
the code. 'reinterpret_cast' is there to show that something drastic is
going on.

Code editors should highlight it in bloody red... or even flashing bloody
red.

I agree when casting classes, but I still like the old style casts when
doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.
 
L

Leor Zolman

I agree when casting classes, but I still like the old style casts when
doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.

Well, if you don't like the new_style cast for aesthetic reasons, or if you
specifically don't want to be warned when the cast isn't doing what you
think it is supposed to do (a benefit of using new-style casts), then
consider this: You have a large app and you wish to locate all the places
you've performed a cast. Tell us how to do it, for certain, with your
old-style casting used all over the place, vs. new-style casts.

Note: assume the coders have been very sloppy with style, writing (type), (
type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.

Note #2: Hope you have a tool supporting good regular expression syntax if
you want to be exhaustive with this ;-)
-leor
 
B

bartek

(...)
I agree when casting classes, but I still like the old style casts
when doing numeric 'type change' cats:

int n1=5;
int n2=3;
double d1 = ((double)n1)/n2;

Where you suggesting a static_cast<> be used here? Personally, I find
that to be a bit much.

Actually in similar cases, where automatic conversion is possible, I'd
suggest using the 'fake' constructor syntax:

double d1 = double(n1) / double(n2);
 
J

Jake Montgomery

Leor said:
Well, if you don't like the new_style cast for aesthetic reasons, or if you
specifically don't want to be warned when the cast isn't doing what you
think it is supposed to do (a benefit of using new-style casts), then
consider this: You have a large app and you wish to locate all the places
you've performed a cast. Tell us how to do it, for certain, with your
old-style casting used all over the place, vs. new-style casts.

Note: assume the coders have been very sloppy with style, writing (type), (
type), ( type ), etc.; don't forget that sizeof(type) will be an imposter.

For simple type conversions (which is what my post spoke of) your
task seems extremely contrived. I can never imagine being concerned
with every occurrence of a int to double cast in the entire app. Could
you come up with a real world situation?
Note #2: Hope you have a tool supporting good regular expression syntax if
you want to be exhaustive with this ;-)
-leor
Yes I do - I use the awesome, but soon-to-be-defunct-due-to-buyout
CodeWright. In the extremely unlikely case that I needed to, I could
indeed do a multiple file grep that would find every occurrence of (say)
a cast to double.
 
J

Jake Montgomery

bartek said:
(...)




Actually in similar cases, where automatic conversion is possible, I'd
suggest using the 'fake' constructor syntax:

double d1 = double(n1) / double(n2);

I have no problem with this syntax ... just habit, and lack of a good
reason to change, keep me using the (double)n1 syntax. I will admit,
that the 'fake' constructor syntax does make things a bit clearer. Are
there any differences between the two in terms of generated code?
 
L

Leor Zolman

For simple type conversions (which is what my post spoke of) your
task seems extremely contrived. I can never imagine being concerned
with every occurrence of a int to double cast in the entire app. Could
you come up with a real world situation?

In the general case, given potentially large-scale projects (code reviews,
etc), the question of how often casts are used /is/ a real-world situation.
If in your project, you know that the only way you're using casts is the
one way you're using them, then you might not personally have this issue.

(And the "task" in question was rather rhetorical, just to point out the
value of the unique keywords used for new-style casts.)
Yes I do - I use the awesome, but soon-to-be-defunct-due-to-buyout
CodeWright. In the extremely unlikely case that I needed to, I could
indeed do a multiple file grep that would find every occurrence of (say)
a cast to double.

Yes, but again, not everyone who might at some point find themselves
needing to locate all their cast operations would necessarily have the
right tool for the job, as well as the requisite skill with r.e.'s, at
their fingertips.
-leor
 
B

bartek

I have no problem with this syntax ... just habit, and lack of a good
reason to change, keep me using the (double)n1 syntax. I will admit,
that the 'fake' constructor syntax does make things a bit clearer. Are
there any differences between the two in terms of generated code?

The compiler I'm using generates exactly the same code in either case.
 
J

Jake Montgomery

Leor said:
In the general case, given potentially large-scale projects (code reviews,
etc), the question of how often casts are used /is/ a real-world situation.
If in your project, you know that the only way you're using casts is the
one way you're using them, then you might not personally have this issue.

(And the "task" in question was rather rhetorical, just to point out the
value of the unique keywords used for new-style casts.)

First off, I just want to re-iterate that I like, advocate for, and use
new_style casts for all situations except the 'trivial' type change
situations I mentioned earlier. That said, please excuse my ignorance
.... my experience has been mostly with (large) applications, but I
really dont see where finding all cats in a large project is a _common_
real world problem.

I would be (honestly) appreciative and enlightened if you would give
me an example of how it would be useful to search on _all_ (non-dynamic)
casts in a large project. (something not too contrived.)

I would be surprised (honestly), but appreciative, if you could give
me a not-overly-contrived example of needing to find all simple numeric
type casts (like those I am 'advocating') in a large project.

Thanx.

[Snip]
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top