Re: Avoid typename repetition in template specialization

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

 



Hi Frank,


On Fri, Apr 09, 2010 at 03:16:27PM +0200, Frank Winter wrote:
> suppose a templated class:
>
> template< typename T >
> struct A {
> 	const T& some_function( const T& )
> 	T i[10];
> };
>
> and a specialization for some type, say
> PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns >:
>
> template<>
> struct A<PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > > {
> 	const PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns >&  
> some_function( const PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns 
> 
>> & )
> 	PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > i[10];
> };
>
> Is there a way to avoid the redundant typing of the specialized typename  
> in the template specialization?
>
> I know, using templates would exactly avoid this. But I really need this 
> kind of specialization to specialize the some_function
>
> I would not like to use the preprocessor. Like:
>
> #define PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > T
> template<>
> struct A<T> {
> 	public:
> 	const T& some_function( const T& )
> 	T i[10];
> }
> #undef T
>
>
> Isn't there a template-way of doing this?

I'm not sure that I understood your question correctly, but the easiest
way I see would be to use a typedef in the specialization -- then it's
sufficient to type the long typename only twice ;-)):

template<>
struct A<PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > > {
  typedef PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > T;
  const T& some_function( const T & );
  T i[10];
};

Or of course you could do the typedef "outside" of the template (this
would however introduce the new name "specialized_type" into your
namespace - which might be problematic if you have many constructs like
that...) :

typedef PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > specialized_type;
template<>
struct A<specialized_type > {
  const specialized_type& some_function( const specialized_type & );
  specialized_type i[10];
};


HTH,

Axel

[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