0

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!

Johannes Gerer
  • 25,508
  • 5
  • 29
  • 35
  • 2
    First, it is not supposed to compile. And you are supposed to file a bug report. There is no question here, and we can't explain bugs in code we don't have the source of. – Alexandre C. Nov 08 '11 at 21:34
  • This also shouldn't compile with the comment removed. – Kerrek SB Nov 08 '11 at 21:39
  • possible duplicate of [C++ syntax for explicit specialization of a template function in a template class?](http://stackoverflow.com/questions/2097811/c-syntax-for-explicit-specialization-of-a-template-function-in-a-template-clas) – Ben Voigt Nov 08 '11 at 21:52
  • @Alexandre C. I will file a bug, if I know that it is a bug. But with C++ it can get quite tricky sometimes – Johannes Gerer Nov 08 '11 at 21:54
  • @user578832: Internal compiler error is *always* a bug. – Alexandre C. Nov 08 '11 at 22:54

2 Answers2

2

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.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
0

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>() {}
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720