Why is C++ said not to support parametric polymorphism?

前端 未结 2 515
清歌不尽
清歌不尽 2021-02-08 04:11

According to the wikipedia page for Parametric Polymorphism:

Some implementations of type polymorphism are superficially similar to parametric polymorphis

2条回答
  •  遥遥无期
    2021-02-08 04:28

    The article you linked to explains that. The very text you quoted actually gives one example of something that sets C++'s templates apart from pure parametric polymorphism: C++ template specialisation.

    It continues on this theme:

    Following Christopher Strachey,[2] parametric polymorphism may be contrasted with ad hoc polymorphism, in which a single polymorphic function can have a number of distinct and potentially heterogeneous implementations depending on the type of argument(s) to which it is applied. Thus, ad hoc polymorphism can generally only support a limited number of such distinct types, since a separate implementation has to be provided for each type.

    Thus, as described, C++ templates come close to — but are not exactly — parametric polymorphism.

提交回复
热议问题