Topic: Template specialization question


Author: "R. Sinoradzki" <sinoradz@student.uni-kl.de>
Date: Thu, 10 May 2001 22:49:07 GMT
Raw View
Hi,

I had the same problem today and I am not sure, if it was my fault
or a bug.

If you comment out 'template<>' it works:

X<char>::type
X<char>::toType(itype i)
{
  return static_cast<type>(i);
}

gcc thinks, that it's a declaration of a specialization if you write
template<> in front of the definition.

I had the same problem when declaring a specialized template class
as a friend.

'template <> friend class foo<int>' causes an error.

'friend class foo<int>;' works.

but perhaps it is me, who is wrong and gcc is correct ...

bye, Ralf


Ryszard Kabatek wrote:
>
> In the sample below I get the folowing error message:
>
> spec.cc:30: template-id `toType<>' for `char X<char>::toType(int)' does
> not match any template declaration
> spec.cc:30: syntax error before `{' token
>
> Is it a compiler (pre gcc-3.0) bug?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Andrei Iltchenko" <iltchenko@yahoo.com>
Date: Thu, 10 May 2001 23:41:18 GMT
Raw View
Ryszard Kabatek <Ryszard.Kabatek@softax.pl> wrote in message
news:3AFA3681.B64C590A@softax.pl...
> In the sample below I get the folowing error message:
>
> spec.cc:30: template-id `toType<>' for `char X<char>::toType(int)' does
> not match any template declaration
> spec.cc:30: syntax error before `{' token
>
> Is it a compiler (pre gcc-3.0) bug?
>
> Regards
> --
> Ryszard Kabatek
>
>
> template<typename T>
> struct X {
>   typedef T type;
>   typedef T itype;
>
>   static type toType(itype i);
>   static itype toItype(type c) {return static_cast<itype>(c);}
>
> };
>
> template<>
> struct X<char> {
>   typedef char type;
>   typedef int itype;
>
>   static type toType(itype i);
>   static itype toItype(type c) {return static_cast<itype>(c);}
> };
>
> template<typename T>
> X<T>::type
> X<T>::toType(itype i)
> {
>   return static_cast<type>(i);
> }
>
> template<>
> X<char>::type
> X<char>::toType(itype i)
> {
>   return static_cast<type>(i);
> }

No, it's not a gcc 3.00 bug at all, as the Standard mandates that members of
an explicitly specialized class template, if they are not defined within the
explicit specialization of their class, be defined without using the
explicit-specialization syntax. So your definition of the static member
function of the explicit class template specialization, should've been
defined as:

X<char>::type
X<char>::toType(itype i)
{
   return static_cast<type>(i);
}


Cheers,

Andrei Iltchenko.


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ryszard Kabatek <Ryszard.Kabatek@softax.pl>
Date: Thu, 10 May 2001 17:24:18 GMT
Raw View
In the sample below I get the folowing error message:

spec.cc:30: template-id `toType<>' for `char X<char>::toType(int)' does
not match any template declaration
spec.cc:30: syntax error before `{' token

Is it a compiler (pre gcc-3.0) bug?

Regards
--
Ryszard Kabatek


template<typename T>
struct X {
  typedef T type;
  typedef T itype;

  static type toType(itype i);
  static itype toItype(type c) {return static_cast<itype>(c);}

};

template<>
struct X<char> {
  typedef char type;
  typedef int itype;

  static type toType(itype i);
  static itype toItype(type c) {return static_cast<itype>(c);}
};

template<typename T>
X<T>::type
X<T>::toType(itype i)
{
  return static_cast<type>(i);
}

template<>
X<char>::type
X<char>::toType(itype i)
{
  return static_cast<type>(i);
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]