What is the difference between casting and conversion?

This question already has an answer here:

  • Is casting the same thing as converting? 12 answers

  • I believe what Eric is trying to say is:

    Casting is a term describing syntax (hence the Syntactic meaning).

    Conversion is a term describing what actions are actually taken behind the scenes (and thus the Semantic meaning).

    A cast-expression is used to convert explicitly an expression to a given type.

    And

    A cast-expression of the form (T)E, where T is a type and E is a unary-expression, performs an explicit conversion (§13.2) of the value of E to type T.

    Seems to back that up by saying that a cast operator in the syntax performs an explicit conversion.


    Casting is a way of telling the compiler "Object X is really Type Y, go ahead and treat it as such."

    Conversion is saying "I know Object X isn't Type Y, but there exists a way of creating a new Object from X of Type Y, go ahead and do it."


    I am reminded of the anecdote told by Richard Feynman where he is attending a philosophy class and the professor askes him "Feynman, you're a physicist, in your opinion is an electron an 'essential object'?" So Feynman asks the clarifying question "is a brick an essential object?" to the class. Every student has a different answer to that question. They say that the fundamental abstract notion of "brickness" is the essential object. No, one specific, unique brick is the essential object. No, the parts of the brick you can empirically observe is the essential object. And so on.

    Which is of course not to answer your question.

    I'm not going to go through all these dozen answers and debate with their authors about what I really meant. I'll write a blog article on the subject in a few weeks and we'll see if that throws any light on the matter.

    How about an analogy though, a la Feynman. You wish to bake a loaf of banana bread Saturday morning (as I do almost every Saturday morning.) So you consult The Joy of Cooking, and it says "blah blah blah... In another bowl, whisk together the dry ingredients. ..."

    Clearly there is a strong relationship between that instruction and your actions tomorrow morning, but equally clearly it would be a mistake to conflate the instruction with the action. The instruction consists of text. It has a location, on a particular page. It has punctuation. Were you to be in the kitchen whisking together flour and baking soda, and someone asked "what's your punctuation right now?", you'd probably think it was an odd question. The action is related to the instruction, but the textual properties of the instruction are not properties of the action.

    A cast is not a conversion in the same way that a recipe is not the act of baking a cake. A recipe is text which describes an action, which you can then perform. A cast operator is text which describes an action - a conversion - which the runtime can then perform.

    链接地址: http://www.djcxy.com/p/22906.html

    上一篇: 声明式编程和命令式编程

    下一篇: 投射和转换有什么区别?