public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Axel Freyn <axel-freyn@gmx.de>
To: gcc-help@gcc.gnu.org
Subject: Re: Avoid typename repetition in template specialization
Date: Fri, 09 Apr 2010 13:42:00 -0000	[thread overview]
Message-ID: <20100409134204.GD14578@axel> (raw)
In-Reply-To: <alpine.LRH.2.00.1004091503370.15744@pub1.ifh.de>

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

  reply	other threads:[~2010-04-09 13:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-09 13:16 Frank Winter
2010-04-09 13:42 ` Axel Freyn [this message]
2010-04-09 13:57 Frank Winter
2010-04-09 14:11 ` John (Eljay) Love-Jensen
2010-04-10 12:51   ` Axel Freyn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100409134204.GD14578@axel \
    --to=axel-freyn@gmx.de \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).