This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Explicity template specialization issue
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: "himanshu.gupta" <himanshu12 dot g at tcs dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 21 Sep 2011 09:14:40 +0100
- Subject: Re: Explicity template specialization issue
- References: <32503712.post@talk.nabble.com>
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