explicit specialization in non-namespace scope

Jonathan Wakely cow@compsoc.man.ac.uk
Tue Jun 17 09:35:00 GMT 2003


On Mon, Jun 16, 2003 at 11:20:54PM -0700, Sujatha, Narayanan (IE10) wrote:
> 
> > I am using gcc 3.1 compiler for power pc. I am getting the following
> > errors if I specialize member templates.

Since this question hasn't been answered on gcc-help I'll do so here...

> > TEST_Stream_accessor.h:22: explicit specialization in non-namespace scope
> > `class TEST::Stream_accessor'

This error is telling you that the explicit specialisation is in a scope
where you are not allowed to make explicit specialisations.

In section temp.expl.spec the standard (*) says:

    An explicit specialization shall be declared in the namespace of which
    the template is a member, or, for member templates, in the namespace of
    which the enclosing class or enclosing class template is a member.

So in this case the explicit specialisation must be in the namespace of
which Stream_accessor is a member.

You must move the specialisation outside the class declaration (and
qualify the function name with Stream_accessor::).

You must also remove the "static" storage class from the specialisation,
only the original declaration should be static, the function definition
and/or specialisations are always static if the declaration says so.

The corrected code is:

namespace TEST
{
   class Stream_accessor
   {
   public:
      Stream_accessor();
      virtual ~Stream_accessor();

      template < class Stream >
      static void create_stream(Stream * * stream_ptr)
      {}
   };

   template < >
   void Stream_accessor::create_stream  < std::istringstream > (std::istringstream * * stream_ptr)
   {
      * stream_ptr = new std::istringstream();
   }
}

VC++ should not have compiled the original code.

hth,

jon

(*) This is actually from a draft of the standard, I don't have the final
version to hand. Caveat lector.

--
Some things have to be believed to be seen.
	- Ralph Hodgson



More information about the Gcc-help mailing list