Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a class template that inherits from a different class template in C++

Sorry for the confusing title, but I'll try to elaborate more here. I haven't been able to find this particular problem via searching, so if I have missed it please point me to the right thread...

I have a class template dependent on one parameter which I am using as a base class:

template <class TVertex>
class DrawExecutorDX11
{
public:
    DrawExecutorDX11( );
    virtual ~DrawExecutorDX11( );
    void AddVertex( const TVertex& vertex );

protected:
    TGrowableVertexBufferDX11<TVertex> VertexBuffer;
};

What I want to do is to inherit from this class template, and at the same time add another class template parameter to the sub-class. My attempt at the syntax would be something like this:

template <class TVertex, class TInstance>
class DrawInstancedExecutorDX11<TInstance> : public DrawExecutorDX11<TVertex>
{
public:
    DrawInstancedExecutorDX11( );
    virtual ~DrawInstancedExecutorDX11( );

    void AddInstance( const TInstance& data );

protected:
    TGrowableVertexBufferDX11<TInstance> InstanceBuffer;
};

I am hoping that this configuration would allow me to declare an instance of the subclass template like so:

DrawInstancedExecutorDX11<VertexStruct,InstanceStruct> myExecutor;

However, VS2012 doesn't even consider compiling the sub-class and indicates that it is expecting a semi-colon after class DrawInstancedExecutorDX11. To be perfectly honest, I haven't ever tried this type of template arrangement before, so I am wondering if anyone else has done this. If so, is there a basic syntax mistake that I am making or some other gotcha? Thanks in advance for any help or guidance you can give!

like image 541
Jason Zink Avatar asked Dec 19 '12 19:12

Jason Zink


1 Answers

If you use any angled-brackets right after the class name, you are declaring a template specialization, not the primary template. The correct primary class template is:

template <class TVertex, class TInstance>
class DrawInstancedExecutorDX11 : public DrawExecutorDX11<TVertex>
{
    //...
};

Inside that class you can refer to template parameters TVertex and TInstance, and outside that class you can use DrawInstancedExecutorDX11<SomeVtxType, SomeInstType>.

like image 200
aschepler Avatar answered Sep 30 '22 15:09

aschepler