1

(The suggested similar questions do not involve template-templates)

I have a templated inheritance hierarchy to share types and behavior.

Grandparent uses types defined by Parent and calls Parent functions. Parent can call Child functions.

Below is a full code example:

https://godbolt.org/z/aW6PPnazG

template<class CONFIG,
         template<class, template<class> class> class PARENT,
         template<class> class CHILD>
struct GrandParent
{
    using PARENT_T = PARENT<CONFIG, CHILD>;

    GrandParent()
    {
        static_cast<PARENT_T*>(this)->ParentFunc();  // Calls Parent functions
    }

    typename PARENT_T::Component _comp;              // Compiler error. PARENT_T is incomplete
};

template<class CONFIG, template<class> class CHILD>
struct Parent : public GrandParent<CONFIG, Parent, CHILD>
{
    using Component = int;
    using CHILD_T = typename GrandParent<CONFIG, Parent, CHILD>::CHILD_T;

    Parent()
    {
        static_cast<CHILD_T*>(this)->ChildFunc();    // Calls Child functions
    }

    void ParentFunc(){}
};

template<class CONFIG>
struct Child : public Parent<CONFIG, Child>
{
    void ChildFunc(){}
};

struct Config{};

int main() 
{
    Child<Config> c;
    return 0;
}

However, I am struggling because I get a compiler error at the very end that PARENT_ is incomplete when I try to get an alias it defined:

<source>:15:34: error: invalid use of incomplete type 'using PARENT_T = struct Parent<Config, Child>' {aka 'struct Parent<Config, Child>'}
   15 |     typename PARENT_T::Component _comp;
      |                                  ^~~~~
<source>:19:8: note: declaration of 'using PARENT_T = struct Parent<Config, Child>' {aka 'struct Parent<Config, Child>'}
   19 | struct Parent : public GrandParent<CONFIG, Parent, CHILD>

The second compiler error refers to Parent inheriting from Grandparent. However, I was hoping to keep using template-template parameters, up until Grandparent to keep Child and Parent classes simpler (because there are many of them) and push the template complications to Grandparent.

What is the best solution?

user997112
  • 29,025
  • 43
  • 182
  • 361
  • *The suggested similar questions do not involve template-templates* It does not matter. The exact error reason is described here https://stackoverflow.com/a/35436437/6752050. The solution with type traits https://stackoverflow.com/a/5680299/6752050 solves your issue. – 273K Dec 03 '22 at 22:55

0 Answers0