为什么在删除复制构造函数时,重载决策不会回退到引用基数 class 的构造函数?

Why does overload resolution not fall back to constructor with reference to base class when copy-constructor is deleted?

作为 的后续问题:

class Base {
    public:
    virtual ~Base() {}
    virtual void func() = 0;
};

class A : public Base {
    public:
     void func() override { std::cout << this << std::endl; }
};

class B : public Base {
    private:
     Base& base;

     public:
      B(B&) = delete;
      B(Base& b) : base(b) {}
      void func() override { std::cout << &base << std::endl; }
};

int main()
{
  A a;
  B b(a);
  B c(b);

  return 0;
}

为什么删除复制构造函数后,重载决策不会回退到引用基 class (B(Base& b)) 的构造函数?

因为= delete是构造函数的一个仍然定义(它被定义为删除)。删除的函数仍然是重载决策的可能候选者,在这种情况下,复制构造函数仍然是最可行的候选者,它被选中了。由于引用了删除的函数,程序格式错误。

根据标准,[dcl.fct.def.delete]:

  1. A deleted definition of a function is a function definition whose function-body is of the form = delete ; [...] A deleted function is a function with a deleted definition or a function that is implicitly defined as deleted.
  2. A program that refers to a deleted function implicitly or explicitly, other than to declare it, is ill-formed.

很容易看出为什么 B::B(B&) 的定义比 B::B(Base&) 类型左值的 B

更可行