Re: Terminology : casting/conversion



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
.



Relevant Pages

  • casting my smart pointer up and down
    ... I am trying to make it behave like a c++ pointer including the implicit ... and explicit casting behaviour. ... Version 1 is fine for casting to a derived class, ... explicitly even though it is used to cast to a base class. ...
    (comp.lang.cpp)
  • Re: malloc and free
    ... There's no such thing as an implicit ... > A conversion is an action that occurs during program execution. ... > can be either explicit (i.e., specified by a cast operator in the ... It specifies an explicit conversion. ...
    (comp.lang.c)
  • Re: srand(time(NULL))
    ... conversion of void * to whatever_type * is implicit). ... you should cast it to void*. ...
    (comp.lang.c)
  • Re: malloc and free
    ... No, it's an implicit conversion. ... A conversion is an action that occurs during program execution. ... specified by a cast operator in the ... It specifies an explicit conversion. ...
    (comp.lang.c)
  • Re: Convert from std::vector<double> to std::vector<int>
    ... >> I fail to see how this is an unnecessary cast. ... What you're talking about is a conversion. ... > The compiler will do the conversion, ... I misdescribed it as casting instead of implicit conversion. ...
    (comp.lang.cpp)