public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Explicity template specialization issue
@ 2011-09-21  5:23 himanshu.gupta
  2011-09-21  8:15 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: himanshu.gupta @ 2011-09-21  5:23 UTC (permalink / raw)
  To: gcc-help


Hi,

The following code is giving errors while compiling with GCC 4.5.

Header File: declaration of template class.

namespace public
{

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...;
        }
    }; //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>'
error:     'ABC'
Error for line with friend class declaratoin:
error: too many template-parameter-lists

The written code was very old and it has been compiled under Linux in the
past. I'm wondering if an older version of GCC supported this, or whether it
is GCC supported extension that can be switched on to compile it
successfully. Did anyone has faced the similar issue? Could you please guide
me how can I resolve this? Is there any compiler option that still allows to
work this syntax?

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.
-- 
View this message in context: http://old.nabble.com/Explicity-template-specialization-issue-tp32503712p32503712.html
Sent from the gcc - Help mailing list archive at Nabble.com.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Explicity template specialization issue
  2011-09-21  5:23 Explicity template specialization issue himanshu.gupta
@ 2011-09-21  8:15 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2011-09-21  8:15 UTC (permalink / raw)
  To: himanshu.gupta; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-09-21  8:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-21  5:23 Explicity template specialization issue himanshu.gupta
2011-09-21  8:15 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).