CRTP child defines type to be used by parent

64 Views Asked by At

I have a CRTP hierarchy, where the child defines a type to be used by the Parent, but that type has a callback in to the Parent:

template<template<class> class CALLBACK>
class Option1
{ 
};

template<class SUB>
class Parent
{
    using ThisClass = Parent<SUB>;
    
    typename SUB::template Sender<ThisClass> _osm;
};

class Child : public Parent<Child>
{
    using Sender = Option1;
};

int main()
{
    Child rm;
}

I thought I could use template template parameters to solve this but I still get compiler errors:

<source>:15:28: error: no member named 'Sender' in 'Child'

Is it possible to achieve this?

1

There are 1 best solutions below

0
Evg On

You can't do it directly because SUB in Parent is an incomplete type, but you can use type traits technique and put Sender type into a separate helper struct:

template<template<class> class Callback>
class Option1 { 
};

class Child;

template<class>
struct GetSender;

template<>
struct GetSender<Child> {
    template<template<class> class Callback>
    using Type = Option1<Callback>;
};

template<class Sub>
class Parent {
    typename GetSender<Sub>::template Type<Parent> _osm;
};

class Child : public Parent<Child> {
};

This works because GetSender doesn't need a complete Sub.