3

I have been reviewing this for a long time without finding an answer, so I decide to ask: What is the difference between conversion, casting and coercion? I'm a bit clumsy with this, I really appreciate the use of a code example.

danielm2402
  • 750
  • 4
  • 14
  • 2
    "I really appreciate the use of a code example." - in what language? You've failed to apply a suitable tag to your question. Also, be aware that there may not be *universal* definitions of these terms, so again a language tag would help immensely. – Damien_The_Unbeliever Feb 26 '21 at 15:17
  • And some confusion may come from the fact that in some languages, the same operator may perform [contradictory actions](https://ericlippert.com/2009/03/03/representation-and-identity/). – Damien_The_Unbeliever Feb 26 '21 at 15:18
  • @Damien_The_Unbeliever It really works for me in any language, I'm just looking to understand the definitions. Still I just added c ++ as a tag – danielm2402 Feb 26 '21 at 15:21
  • Casting is an explicit conversion – alex_noname Feb 26 '21 at 15:21
  • 3
    @danielm2402: "*It really works for me in any language,*" That's not possible, because these words are *different* for different languages. And many languages don't have some of these words/concepts at all. – Nicol Bolas Feb 26 '21 at 15:23
  • 2
    Does this answer your question? [What is the difference between casting and coercing?](https://stackoverflow.com/questions/8857763/what-is-the-difference-between-casting-and-coercing) – anastaciu Feb 26 '21 at 15:28
  • @anastaciu Not really, I understand that there is also conversion, and the answers caused me more confusion than clarification :( – danielm2402 Feb 26 '21 at 15:32
  • 2
    The term "coercion" is not used in C++. There's "implicit conversion" and "explicit conversion" ("casting"), and "casting" is often misused for all conversions (although "implicit casting" is an oxymoron). The terminology is different in different languages, and you need to study the definition of the ones you're interested in. – molbdnilo Feb 26 '21 at 15:33
  • @danielm2402 the post also approaches conversion, anyway, I don't know what to tell you, they seem quite clear to me. Maybe someone can clarify your doubts with an answer, in any case it would help if detail what exactly is causing your confusion otherwise it's hard to explain. – anastaciu Feb 26 '21 at 15:36
  • imho its a good question, but you need to get rid of the false premise that definitions of the terms are langague agnostic (cf. eg one of the answers in the proposed dupe that starts with "My personal usages are:"). Colloquial language is one thing, if you care about definitions you have to recognize that every langauge has their own definitions – 463035818_is_not_an_ai Feb 26 '21 at 15:37

1 Answers1

3

[C++] What is the difference between conversion, casting and coercion?

  • Conversion is the creation of a value from an expression, potentially changing the type of the resulting value.

  • A cast is an explicit way to convert a value. Some conversions do not require a cast. Those are called implicit conversions. Example of an implicit conversion:

    int a = 42;
    long b = a; // type of expression a is int,
                // but implicitly converted to long
    

    Same example using a cast to make the conversion explicit:

    long b = static_cast<long>(a);
    
  • Type "coercion" is colloquially used as a synonym of conversion, but the term is not used in the specification of the C++ language. The term is used for example in the ECMAScript language (i.e. JavaScript). Example of a coercion in JS:

    Number("42") + 42  // == 84
    "42" + 42          // == "4242"
    
eerorika
  • 232,697
  • 12
  • 197
  • 326