سؤال

I am conducting a research on type systems. For this work I am investigating the usages of Variants, structural subtyping, universal polymorphism and existential polymorphism in popular languages. Functional languages like heskell, ocaml provides such functionaries. But I want to whether a popular language like C++ provide above functionality. That means how C++ implemented

  1. variants

  2. structural subtyping

  3. universal polymorphism

  4. existential polymorphism.

هل كانت مفيدة؟

المحلول

  1. Unions can be viewed as a rudimentary form of variant, but in reality, they are more a primitive mechanism for overlaying memory (and unsafe).

  2. There is no structural typing, let alone subtyping, in C++. All types are nominal.

  3. Templates have some superficial similarity to universal polymorphism, but are actually quite different. In essence, they are glorified macros with little to no type checking (like with macros, both checking and code generation happens after expansion).

  4. There is no form of existential types in C++ (there is a limited form in Java, namely wildcards).

Some of these features can be simulated to some extent using subtyping, but that remains far less expressive (or convenient).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top