Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interdependent class templates design?

Consider the following design :

template <class SecondType>
struct First
{
    SecondType* _ptr;
};

template <class FirstType>
struct Second
{
    FirstType* _ptr;
};

where the First type has a pointer to a Second type and vice-versa. The problem is that I cannot declare this because they are interdependent and I should declare First<Second<First<Second...>>>.

How to solve this problem ?

like image 273
Vincent Avatar asked Nov 02 '22 21:11

Vincent


1 Answers

Maybe a work-around with something that looks like CRTP but even crazier:

#include <iostream>

template <class SecondType>
struct FirstBase
{
    SecondType* _ptr;
};

template <class FirstType>
struct SecondBase
{
    FirstType* _ptr;
};

struct FirstDerived
: public FirstBase<SecondBase<FirstDerived>>
{
};

struct SecondDerived
: public SecondBase<FirstBase<SecondDerived>>
{
};

int main()
{
    FirstBase<SecondDerived> x;
    SecondBase<FirstDerived> y;
    return 0;
}

If someone has a more elegant way to do this, I would be happy to see it.

like image 116
Vincent Avatar answered Nov 12 '22 15:11

Vincent