#1316 template of inherited type is impossible

open
nobody
None
5
2014-07-30
2013-04-19
Job
No

I have a construction which is often used in C++ where my template class has a parameter that is the inheriting type (see simplified code below). This is required for some iterators and stream operators etc to work properly without a huge amount of code duplication.

If I don't use %template(DowncastImpl) Downcastable<Downcasted>; I get these warnings:
Warning 401: Nothing known about base class 'Downcastable< Downcasted >'. Ignored.
Warning 401: Maybe you forgot to instantiate 'Downcastable< Downcasted >' using %template.

But if I do use %template(DowncastImpl) Downcastable<Downcasted>; I get these warnings:
Warning 401: Base class 'Downcastable< Downcasted >' undefined.
Warning 401: 'Downcastable< Downcasted >' must be defined before it is used as a base class.

In my actual implementation SWIG goes into some kind of eternal loop, probably because I have a class that uses this pattern and which contains a nested Iterator definition that also uses this pattern.

Since the relevant structure is the base structure for half my interface objects (it's an array container), I can't use swig for my interface generation while this problem persists.

template <typename I>
struct Downcastable
{
    Downcastable<I>() {}
    virtual ~Downcastable<I>() {}

    I* derived()
    {
        return static_cast<I*>(this);
    }
};

struct Downcasted : public Downcastable<Downcasted>
{
    Downcasted() {}
    ~Downcasted() {}

    Downcasted* me()
    {
        return derived();
    }
};

Discussion