Search code examples
c++template-specializationvisual-c++-2012

Specialize C++ class template with final specifier


I have a template and a specialization defined like this:

template<typename T>
struct SomeTemplate final
{
};

template<>
struct SomeTemplate<int> final
{
};

int main()
{
    SomeTemplate<int> test; // <-- error
}

Compiling with Visual C++ 2012 gives me the following error:

error C2913: explicit specialization; 'SomeTemplate<T>' is not a specialization of a class template
    with
    [
        T=int
    ]

It compiles fine and does the right thing when I remove

  • the final specifiers
  • the template<> from the specialization

The first case makes some sense to me, as it may very well be that final does also restrict specialization of templates (instead of only inheritance). The second case seems a bit strange to me, as I thought this should be a syntax error.

Is this behavior correct?


Solution

  • I'm not sure weather it's a bug of Visual C++ 2012, your original code compiles fine with the newer version of VC++ here.

    1. as it may very well be that final does also restrict specialization of templates (instead of only inheritance)

    No, final specifier has nothing to do with template specialization.

    Specifies that a virtual function cannot be overridden in a derived class or that a class cannot be inherited from.

    And

    1. The second case seems a bit strange to me, as I thought this should be a syntax error.

    Yes it should be a syntax error if you remove template<>.