静态选择的功能和虚拟功能

str*_*yku 2 c++ virtual-functions static-typing language-lawyer covariant-return-types

最近我看到了这个C++标准段落(http://eel.is/c++draft/expr.post#expr.call-5):

如果postfix-expression指定析构函数,则函数调用表达式的类型为void; 否则,函数调用表达式的类型是静态选择函数的返回类型(即,忽略虚拟关键字),即使实际调用的函数的类型不同.此返回类型应为对象类型,引用类型或cv void.

我真的不明白这一部分:

函数调用表达式的类型是静态选择函数的返回类型(即,忽略虚拟关键字),即使实际调用的函数的类型不同.

  1. 这里静态选择的功能是什么?
  2. 虚拟功能如何静态选择?我一直以为它是在运行时选择的.
  3. 即使实际调用的函数类型不同.

调用表达式如何实际调用所选择的不同类型的函数?

Jar*_*d42 7

虚函数可以有协变返回类型,

所以

struct Base
{
    virtual ~Base() = default;
    virtual Base* Clone() const { return new Base(*this); }
};

struct Derived : Base
{
    // covariant return type:
    Derived* Clone() const override { return new Derived(*this); }
};
Run Code Online (Sandbox Code Playgroud)

然后

Derived d;

Base& b = d;

auto* clonePtr = b.Clone(); // `auto` is `Base`, even if `Derived::Clone` is called.
                         // runtime type of `clonePtr` is `Derived`
std::unique_ptr<Base> clone(clonePtr); // Done in 2 steps for explanation
Run Code Online (Sandbox Code Playgroud)