Re: Terminology : casting/conversion
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 11 Mar 2006 16:55:59 -0500
Richard G. Riley wrote:
If I do
p = (char *)p2;
Is this an "explicit" cast? Or just a cast?
It is a "cast," and it is "explicit," but "explicit cast"
is overkill. In `a + b' would you call the `+' an "explicit
addition operator?"
p = p2;
Here are p & p2 are pointers but the compiler "implicitly" converts
one to the other since they are different base pointer types.
Assuming that they're assignment-compatible; if `p' is
a `double*' and `p2' is a `char*', this would require a
diagnostic. But yes: If they're something like `double*'
and `void*', the conversion is implicit.
Now to my really confused mind, it would be ok to call this an
"implicit cast"?
No; there's no such thing. It's an implicit conversion,
that is, a conversion that occurs in the absence of an operator
(all operators are explicit).
I think that you (and you're not by any means alone) have
somehow stirred the two different notions of "conversion" and
"cast" into the same can of muddy-colored paint. If you make
a point of separating them:
- A "conversion" is the act of deriving a value of one type
from a value of another. Some conversions (`int' -> `long',
`char*' -> `void*', others) occur automatically when the
context requires them; these are "implicit" conversions.
- A "cast" is an operator that causes a conversion (except
in degenerate cases like casting an `int' to an `int', or
casting anything to `void'). Since it is written out in
the source code, right there where anyone can see it, it
is necessarily "explicit."
.... your confusion may diminish.
A single expression can have both implicit and explicit
conversions, e.g. `(double)5 / 9': the conversion of five from
`int' to `double' is explicit and caused by the cast, while
the conversion of nine from `int' to `double' is implicit,
implied by the rules of the division operator. And, of course,
an expression can also have unnecessary explicit conversions,
as in `(double)5 / (double)9' -- there can be sometimes be
value in inserting extra casts as a kind of documentation.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: Terminology : casting/conversion
- From: Richard G. Riley
- Re: Terminology : casting/conversion
- References:
- Terminology : casting/conversion
- From: Richard G. Riley
- Re: Terminology : casting/conversion
- From: Michael Mair
- Re: Terminology : casting/conversion
- From: Richard G. Riley
- Re: Terminology : casting/conversion
- From: Richard Heathfield
- Re: Terminology : casting/conversion
- From: Richard G. Riley
- Terminology : casting/conversion
- Prev by Date: Re: Terminology : casting/conversion
- Next by Date: Re: global variables are bad?
- Previous by thread: Re: Terminology : casting/conversion
- Next by thread: Re: Terminology : casting/conversion
- Index(es):
Relevant Pages
|