为什么这些方法调用不明确?

Ten*_*jix 4 c++ multiple-inheritance ambiguity member-functions name-lookup

#include <string>
using String = std::string;

class Base {
protected:
    String value;
};

class Readonly : virtual Base {
public:
    const String& method() const {
        return value;
    }
    String& method() {
        return value;
    }
};

class Writeonly : virtual Base {
public:
    Writeonly& method(const String& value) {
        this->value = value;
        return *this;
    }
    Writeonly& method(String&& value) {
        this->value = std::move(value);
        return *this;
    }
};

class Unrestricted : public Readonly, public Writeonly {};

void usage() {
    String string;
    Unrestricted unrestricted;
    unrestricted.method(string); // ambiguous
    string = unrestricted.method(); // ambiguous
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我解释为什么这些方法调用是模棱两可的?

当它们以"Writeonly"或"Readonly"组合在一起时,它们并不含糊.

我想将它用于基于模板的访问器属性.因此,我希望能够使用"Writeonly","Readonly"和"Unrestricted"的实例.

Ric*_*ges 6

因为编译器在两个不同的范围中找到了该名称.

诀窍是将两个名称都纳入以下范围Unrestricted:

class Unrestricted : public Readonly, public Writeonly {
  public:
  using Readonly::method;
  using Writeonly::method;
};
Run Code Online (Sandbox Code Playgroud)