Re: Explicity template specialization issue

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 21 September 2011 06:22, himanshu.gupta wrote:
>
> Hi,
>
> The following code is giving errors while compiling with GCC 4.5.
>
> Header File: declaration of template class.
>
> namespace public

'public' is not a valid namespace name.

> {
>
> template<class T>
> class Z
> {
> }; //Z
>
> template<class ABC>
> class X
> {
> protected:
>    template<class T>
>    class Y: public Z<T>
>    {
>        void MyFunc( X<ABC>& base )
>        {
>            do somthing...;

This isn't valid C++ either, it helps if you post real example code
that is what you're actually trying to compile e.g.
              // do something

>        }
>    }; //Y
>
>    // Explicit specialization for "void" class.
>    template<>
>    class Y<void>: public Z<void>
>    {
>        void MyFunc( X<ABC>& base )
>        {
>            do somthing else...;
>        }
>    }; //Y explicit specialization
>
>    template<typename T> friend class Y;
> }; //X
>
> } // public
>
> The above explicit inner class declaration for "void" is giving errors...
> Errors for the line after the code comment of Explicity specialization):
> error: explicit specialization in non-namespace scope 'class public::X<ABC>'

This is telling you that you can't declare the specialization there,
it must be at namespace-scope not at class-scope.

If you move it to class scope you will get a different error because
(as you say below) you cannot specialize an inner template without
specializing the outer class template.

> Note that the C++ standard says you cannot specialise the inner template
> without specialising the outer template. But I can not declare/define inner
> template class Y outside as it is using the outer template class parameter
> for the function.

Yes you can:

template<>
template<>
class X<int>::Y<void>: public Z<void>
{
  void MyFunc( X<int>& base )
  {
  }
}; //Y explicit specialization



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux