C++:私有继承模糊调用

c++: private inheritance ambiguous call

在下面的代码中,public 方法 Base2::f() 应该成为 Derived class 的私有成员,但是编译器抱怨不明确。

题目是对继承的基本理解。如果有人可以帮助阐明它,我将不胜感激。

#include <iostream>

template <typename TDerived>
class Base1 {
 public:
  void f() { static_cast<TDerived &>(*this)->f_impl(); }
};

class Base2 {
 public:
  void f() { std::cout << "f()" << std::endl; }
};

class Derived : public Base1<Derived>, private Base2 {
 public:
  void f_impl() { std::cout << "f_impl()" << std::endl; }
};

int main() {
  Derived d;

  d.f();
}

Jarod42 rightfully posted the

The check of visibility (public/private) is done after the selection, and so it is ambiguous

这是cppreference

中的相关部分

Member access does not affect visibility: names of private and privately-inherited members are visible and considered by overload resolution, implicit conversions to inaccessible base classes are still considered, etc. Member access check is the last step after any given language construct is interpreted. The intent of this rule is that replacing any private with public never alters the behavior of the program.