0

I have a set of classes implementing the curiously recurring template pattern. However, the trick is that the base class needs to return instances of the subclasses. Here's an example:

template <typename SubType>
class ArithmeticBase
{
public:
    template <typename OtherType>
    const Addition operator+(const OtherType &other)
        {return Addition(get_subclass(), other);}

    // ...
    // Operators for subtraction, multiplication, division, ...

private:
    const SubType &get_subclass() const
        {return *static_cast<const SubType*>(this);}
};

template <typename OperatorType1, typename OperatorType2>
class Addition : ArithmeticBase<Addition<OperatorType1, OperatorType2>>
{
public:
    Addition(const OperatorType1 &op1, const OperatorType2 &op2)
        : op1(op1)
        , op2(op2)
    {}

private:
    const OperatorType1 &op1;
    const OperatorType2 &op2;
};

// ...
// Additional classes for subtraction, multiplication, division, ...

Compiling this fails because the Addition class is not defined before it's used in the ArithmeticBase class:

arithmetic.cpp:6:8: error: unknown type name 'Addition'
        const Addition operator+(const OtherType &other)
              ^

How can I resolve this?

Joel
  • 2,654
  • 6
  • 31
  • 46

3 Answers3

4

You could forward declare Addition before the base class.

template <typename OperatorType1, typename OperatorType2>
class Addition;
template <typename SubType>
class ArithmeticBase
{
...
};

This allows the compiler to know there is a type Addition that exists before it is defined.

bhzag
  • 2,932
  • 7
  • 23
  • 39
2

Or use non-member form declared after Addition:

template <typename OperatorType1, typename OperatorType2>
class Addition;

template <typename SubType>
class ArithmeticBase
{
public:

     template <typename OneType, typename OtherType>
     friend const Addition<OneType, OtherType> operator+(const ArithmeticBase<OneType>& one, const OtherType &other);

private:
    const SubType &get_subclass() const
    {
        return *static_cast<const SubType*>(this);
    }
};

class ArithmeticType : public ArithmeticBase < ArithmeticType > {};

template <typename OperatorType1, typename OperatorType2>
class Addition : ArithmeticBase<Addition<OperatorType1, OperatorType2>>
{
public:
    Addition(const OperatorType1 &op1, const OperatorType2 &op2)
        : op1(op1)
        , op2(op2)
    {}

private:
    const OperatorType1 &op1;
    const OperatorType2 &op2;
};


template <typename OneType, typename OtherType>
const Addition<OneType, OtherType> operator+(const ArithmeticBase<OneType>& one, const OtherType &other)
{
    return Addition<OneType, OtherType>(one.get_subclass(), other);
}

int main()
{
    ArithmeticType a, b;
    a + b;
}
Alexander Balabin
  • 2,055
  • 11
  • 13
1

In addition to forward declaring the Addition class (as bhzag's answer shows) you'll need to move the definition of operator+ to after the definition the Addition class. Otherwise you'll get an error on the next line.

Make sure the definition is in the header file. If it isn't, you'll get linker errors.

JKor
  • 3,822
  • 3
  • 28
  • 36