0

I am trying to port some code from a visual studio solution to GCC. The code looks like this and I am trying to understand why I am getting this issue

template <class BASE, class SHADER>
class Shader : public BASE {
...
...
    ~Shader()
    {
        someAssesrt(0);
    }
};


typedef Shader< VertexShader, ddVertexShader> VertexShaderBase;

VertexShaderBase::~VertexShaderBase()  <-- ERROR HERE
{
}

The error is:

error: specializing member '{anonymous}::Shader< VertexShader, IDirect3DVertexShader9*>::~ <unnamed>::VertexShaderBase< VertexShader, IDirect3DVertexShader9*>' requires 'template<>' syntax

I am having difficulty understanding this error and some other code components.

First of all what is happening here

typedef Shader< VertexShader, ddVertexShader> VertexShaderBase;

    VertexShaderBase::~VertexShaderBase()  <-- ERROR HERE
    {
    }

From my understanding a type of VertexShaderBase is created and then the destructor of that type is implemented. Isnt the destructor of the type already defined in the type Shader ? If i understand that then maybe I might be able to figure out why I am getting this error. From what I understand is that Shader<VertexShader, ddVertexShader> is a type somewhere in the code ?

 error: specializing member '{anonymous}::Shader< VertexShader, IDirect3DVertexShader9*>::~ <unnamed>::VertexShaderBase< VertexShader, IDirect3DVertexShader9*>' requires 'template<>' syntax

I am not sure what is happening here and help regarding understanding this code would be helpful.

MistyD
  • 16,373
  • 40
  • 138
  • 240
  • 1
    The error message seems self-explanatory... what happened when you tried with `template<>` in front? – Ben Voigt Mar 13 '15 at 23:14
  • It works however i am trying to understand the code too and why that is happening – MistyD Mar 13 '15 at 23:16
  • 1
    `Shader` is a template for defining classes, `VertexShaderBase` aka `Shader< VertexShader, ddVertexShader>` is a particular instance of that template. When you change the behavior of one instance of the template, that's called **specialization**. If you change the behavior of an entire category of instances at once, that's *partial specialization*. – Ben Voigt Mar 13 '15 at 23:17
  • Thanks Ben let me see if I get this correct. So there was a destructor in the shader class. Then there was the destructor that i mentioned above. That destructor of the shader class only gets called if the template parameter of the shader class are of types ` VertexShader, ddVertexShader ` . I understand the destructor of the class like any other method was being specialized. Kindly correct me if I am wrong – MistyD Mar 13 '15 at 23:21
  • @MistyD yes that's right – M.M Mar 13 '15 at 23:37

1 Answers1

0

You always need to inform the compiler that you are specializing a template by placing template<> in front of it. If it is a partial specialization it might be template<class X> instead.

If the Microsoft compiler was not requiring this, then it was doing it wrong.

The syntax gets especially interesting when specializing a template function of a template class. See https://stackoverflow.com/a/4995106/13422

Community
  • 1
  • 1
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131