Polymorphism in C++

后端 未结 7 1075
甜味超标
甜味超标 2020-11-22 09:01

AFAIK:

C++ provides three different types of polymorphism.

  • Virtual functions
  • Function name overloading
  • Operator overloading
7条回答
  •  暖寄归人
    2020-11-22 09:35

    In C++, the important distinction is run-time vs. compile-time binding. Ad-hoc vs. parametric doesn't really help, as I'll explain later.

    |----------------------+--------------|
    | Form                 | Resolved at  |
    |----------------------+--------------|
    | function overloading | compile-time |
    | operator overloading | compile-time |
    | templates            | compile-time |
    | virtual methods      | run-time     |
    |----------------------+--------------|
    

    Note - run-time polymorphism may still be resolved at compile-time, but that's just optimization. Needing to support run-time resolution efficiently, and trading off against other issues, is part of what led to virtual functions being what they are. And that's really key for all forms of polymorphism in C++ - each arises from different sets of trade-offs made in a different context.

    Function overloading and operator overloading are the same thing in every way that matters. The names and the syntax for using them doesn't affect polymorphism.

    Templates allow you to specify lots of function overloads at once.

    There's another set of names for the same resolution-time idea...

    |---------------+--------------|
    | early binding | compile-time |
    | late binding  | run-time     |
    |---------------+--------------|
    

    These names are more associated with OOP, so it's a bit odd to say that a template or other non-member function uses early binding.

    To better understand the relationship between virtual functions and function overloading, it's also useful to understand the difference between "single dispatch" and "multiple dispatch". The idea can be understood as a progression...

    • First, there are monomorphic functions. The implementation of the function is uniquely identified by the function name. None of the parameters is special.
    • Then, there is single dispatch. One of the parameters is considered special, and used (along with the name) to identify which implementation to use. In OOP, we tend to think of this parameter as "the object", list it before the function name etc.
    • Then, there is multiple dispatch. Any/all parameters contribute to identifying which implementation to use. Therefore, once again, none of the parameters needs to be special.

    There's obviously more to OOP than an excuse to nominate one parameter as special, but that is one part of it. And relating back to what I said about trade-offs - single dispatch is quite easy to do efficiently (the usual implementation is called "virtual tables"). Multiple dispatch is more awkward, not just in terms of efficiency, but also for separate compilation. If you're curious, you might look up "the expression problem".

    Just as it's a bit odd to use the term "early binding" for non-member functions, it's a bit odd to use the terms "single dispatch" and "multiple dispatch" where polymorphism is resolved at compile-time. Usually, C++ is considered not to have multiple dispatch, which is considered a particular kind of run-time resolution. However, function overloading can be seen as multiple-dispatch done at compile-time.

    Getting back to parametric vs. ad-hoc polymorphism, these terms are more popular in functional programming, and they don't quite work in C++. Even so...

    Parametric polymorphism means that you have types as parameters, and the exact same code is used irrespective of what type you use for those parameters.

    Ad-hoc polymorphism is ad-hoc in the sense that you provide different code depending on the particular types.

    Overloading and virtual functions are both examples of ad-hoc polymorphism.

    Again, there's some synonyms...

    |------------+---------------|
    | parametric | unconstrained |
    | ad-hoc     | constrained   |
    |------------+---------------|
    

    Except these aren't quite synonyms, though they're commonly treated as though they were, and that's where confusion is likely to arise in C++.

    The reasoning behind treating these as synonyms is that by constraining polymorphism to particular classes of types, it becomes possible to use operations specific to those classes of types. The word "classes" here can be interpreted in the OOP sense, but really just refers to (usually named) sets of types that share certain operations.

    So parametric polymorphism is usually taken (at least by default) to imply unconstrained polymorphism. Because the same code is used irrespective of the type parameters, the only supportable operations are those that work for all types. By leaving the set of types unconstrained, you severely limit the set of operations you can apply to those types.

    In e.g. Haskell, you can have...

    myfunc1 :: Bool -> a -> a -> a
    myfunc1 c x y = if c then x else y
    

    The a here is an unconstrained polymorphic type. It could be anything, so there's not much we can do with values of that type.

    myfunc2 :: Num a => a -> a
    myfunc2 x = x + 3
    

    Here, a is constrained to be a member of the Num class - types that act like numbers. That constraint allows you to do number-ish things with those values, such as add them. Even the 3 is polymorphic - type inference figures out that you mean the 3 of type a.

    I think of this as constrained parametric polymorphism. There's only one implementation, but it can only be applied in constrained cases. The ad-hoc aspect is the choice of which + and 3 to use. Each "instance" of Num has it's own distinct implementation of these. So even in Haskell "parametric" and "unconstrained" aren't really synonyms - don't blame me, it's not my fault!

    In C++, both overloading and virtual functions are ad-hoc polymorphism. The definition of ad-hoc polymorphism doesn't care whether the implementation is selected at run-time or compile-time.

    C++ gets very close to parametric polymorphism with templates if every template parameter has type typename. There are type parameters, and there's a single implementation no matter which types are used. However, the "Substitution Failure Is Not An Error" rule means that implicit constraints arise as a result of using operations within the template. Additional complications include template specialization for providing alternative templates - different (ad-hoc) implementations.

    So in a way C++ has parametric polymorphism, but it's implicitly constrained and could be overridden by ad-hoc alternatives - ie this classification doesn't really work for C++.

提交回复
热议问题