警告:由于不明确,派生中无法访问直接基类;这严重吗?

pre*_*eys 1 c++ virtual inheritance ambiguity

我不明白这里有什么歧义。我确实确定了导致歧义的行并对其进行了标记。

#include <string>
#include <unordered_map>

class Spell {
    protected:
        struct Exemplar {};
        Spell() = default;
        Spell (Exemplar, const std::string&);
};

class SpellFromScroll : virtual public Spell {
    private:
        static std::unordered_map<std::string, SpellFromScroll*> prototypesMap;
    public:
        static void insertInPrototypesMap (const std::string& tag, SpellFromScroll* spell) {
            prototypesMap.emplace (tag, spell);
        }
        template <typename T> static SpellFromScroll* createFromSpell (T*);
};
std::unordered_map<std::string, SpellFromScroll*> SpellFromScroll::prototypesMap;

class SpellWithTargets : virtual public Spell {};  // *** Note: virtual

class Sleep : public SpellWithTargets {
    private:
        static const Sleep prototype;
    public:
        static std::string spellName() {return "Sleep";}
    private:
        Sleep (Exemplar e) : Spell (e, spellName()) {}
};
const Sleep Sleep::prototype (Exemplar{});

template <typename T>
class ScrollSpell : /*virtual*/ public T, public SpellFromScroll {};

Spell::Spell (Exemplar, const std::string& spellName) {
    // Ambiguity warning!
    SpellFromScroll::insertInPrototypesMap (spellName, SpellFromScroll::createFromSpell(this));
}

template <typename T>
SpellFromScroll* SpellFromScroll::createFromSpell (T*) {
    return new ScrollSpell<T>;
}

int main() {}

/*
c:\ADandD>g++ -std=c++14 Ambiguity.cpp -o a.exe -Wall -Wextra -pedantic-errors
Ambiguity.cpp: In instantiation of 'class ScrollSpell<Spell>':
Ambiguity.cpp:32:13:   required from 'static SpellFromScroll* SpellFromScroll::createFromSpell(T*) [with T = Spell]'
Ambiguity.cpp:27:90:   required from here
Ambiguity.cpp:23:7: warning: direct base 'Spell' inaccessible in 'ScrollSpell<Spell>' due to ambiguity
 class ScrollSpell : public T, public SpellFromScroll {};
       ^
Ambiguity.cpp:23:7: warning: virtual base 'Spell' inaccessible in 'ScrollSpell<Spell>' due to ambiguity [-Wextra]

c:\ADandD>
*/
Run Code Online (Sandbox Code Playgroud)

问题有多严重?随着程序的发展,以后可能会出现什么问题?

更新:通过让 T 成为 的虚拟基找到了一个解决方案ScrollSpell<T>。但在我的程序中,T 始终是 Spell 的派生类,而 Spell 始终是 T 的虚拟基。请参见下图。

                  Spell
                  /   \
              v  /     \ v
                /       \
               /         \  
   SpellFromScroll      SpellWithTargets
          \                \
           \                \
            \               Sleep
             \               /
              \             / v
               \           / 
             ScrollSpell<Sleep>
Run Code Online (Sandbox Code Playgroud)

上图中,为什么Sleep做一个虚拟基地就能ScrollSpell<Sleep>解决问题呢?

Pet*_*r B 5

template <typename T>
class ScrollSpell : public T, public SpellFromScroll {};
Run Code Online (Sandbox Code Playgroud)

在这里,T = Spell,因此该类ScrollSpell将该Spell类作为直接的非虚拟基类,并且还通过 将该类作为虚拟基类SpellFromScroll。这就是歧义。将基类声明T为 virtual 可能会解决问题。

另外,我不太了解设计背后的要点,因此这可能会带来一些全新的问题。