Johannes Gerer
Johannes Gerer

Reputation: 25818

Strange Error with Partial and explicit template specialization

The following gives an "Internal Compiler Error" on MSVC++ 10 Sp1.

And on gcc:

b.cpp:16:12: error: explicit specialization in non-namespace scope ‘struct A::B’

b.cpp:16:28: error: template-id ‘f’ in declaration of primary template

//class template
template< class T>
struct A{

    //struct B  {};  //Remove the comment and it will compile!
};

//partial specialization
template<  class T >
struct A< T* >
{
    struct B  {

        template<class C>   void f(){}

            //"Internal Compiler Error"
        template<>          void f<int>(){};

    };
};

However, if the comments before struct B is removed it will compile!

I don't understand the problem!

Upvotes: 0

Views: 396

Answers (2)

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

You have a bug in your code and MSVC++ isn't coping with it. The gcc compile produces this:

$ make parspec.o
g++    -c -o parspec.o parspec.cc
parspec.cc:17: error: explicit specialization in non-namespace scope ‘struct A<T*>::B’
make: *** [parspec.o] Error 1

In short, you can't specialise inside a class or struct.

EDIT: A quick Google around suggests that MSVC++ allows such non-conforming constructs, but I guess they didn't do a very good job of it.

Upvotes: 2

Ben Voigt
Ben Voigt

Reputation: 283644

You cannot specialize a member function for multiple instances of a class template. This is allowed:

template<typename T>
struct A{
};

template<typename T>
struct A<T*>
{
    struct B  {
        template<class C>   void f();
    };
};

template<typename T>
template<typename C>
void A<T*>::B::f() {}

template<>
template<>
void A<char*>::B::f<int>() {}

But this is not:

template<typename T>
template<>
void A<T*>::B::f<int>() {}

Upvotes: 0

Related Questions