Search code examples
c++inheritancevirtualambiguity

Warning: direct base class inaccessible in derived due to ambiguity; is this serious?


I don't understand what the ambiguity is here. I did identify the line that causes the ambiguity and marked it.

#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>
*/

How serious is it, and what can go wrong later on as the program evolves?

Update: A solution is found by letting T be a virtual base of ScrollSpell<T>. But in my program T is always a derived class of Spell, and Spell is always a virtual base of T. See the diagram below.

                  Spell
                  /   \
              v  /     \ v
                /       \
               /         \  
   SpellFromScroll      SpellWithTargets
          \                \
           \                \
            \               Sleep
             \               /
              \             / v
               \           / 
             ScrollSpell<Sleep>

In the above diagram, why does Sleep being a virtual base of ScrollSpell<Sleep> solve the problem?


Solution

  • template <typename T>
    class ScrollSpell : public T, public SpellFromScroll {};
    

    Here, T = Spell, so the ScrollSpell class has the Spell class as a direct, non-virtual base class, and also as a virtual base class through SpellFromScroll. That is the ambiguity. Declaring the base class T as virtual might solve the problem.

    Also I don't really understand the point behind the design, so that might introduce some completely new issues.