Jason Zink
Jason Zink

Reputation: 100

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!

Upvotes: 5

Views: 144

Answers (2)

K-ballo
K-ballo

Reputation: 81349

You are trying to declare a partial specialization without having declared the primary template definition. Before the declaration of DrawInstancedExecutorDX11<TInstance> you would need

template <class TVertex, class TInstance>
class DrawInstancedExecutorDX11;

That should get ride of your first compiler error, but fail afterwards. I'm not sure what you are actually trying to accomplish with this. It seems that you simply need:

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

Upvotes: 1

aschepler
aschepler

Reputation: 72356

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>.

Upvotes: 4

Related Questions