C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

partial template specialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Bruno van Dooren
Guest





PostPosted: Thu May 18, 2006 8:21 pm    Post subject: partial template specialization Reply with quote



Hi all,
I have a template class for which I want to provide a specialization.
there are 2 ways to do this, and I have the impression that they are 100%
equivalent.
So I was wondering: is there any difference, or perhaps one is preferred
over the other:

//option 1
template< class T , int Q=1> class test { ... };
template< class T > class test<T> {};

//option 2
template< class T , int Q> class test{ ... };
template< class T > class test<T, 1>{ ... };

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren (AT) hotmail (DOT) com
Remove only "_nos_pam"

---
[ 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.comeaucomputing.com/csc/faq.html ]
Back to top
Greg Herlihy
Guest





PostPosted: Fri May 19, 2006 12:21 pm    Post subject: Re: partial template specialization Reply with quote



Bruno van Dooren wrote:
Quote:
Hi all,
I have a template class for which I want to provide a specialization.
there are 2 ways to do this, and I have the impression that they are 100%
equivalent.
So I was wondering: is there any difference, or perhaps one is preferred
over the other:

//option 1
template< class T , int Q=1> class test { ... };
template< class T > class test<T> {};

I find the second declaration here to be quite disconcerting. In fact
the declaration does not look like it should compile - and it indeed it
would not compile - save for the existence of the class template
declaration in the first line. As it turns out, the second declaration
is actually a partial specialization for a class template, but
specialized for a parameter that is entirely absent from the
declaration - certainly an unusual situation to say the least.

So even though the second declaration is perfectly legal - the fact
that it looks so questionable would - in my mind - strongly argue
against its use. On the other hand, declaring a default non-type
parameter can certainly be a worthwhile notational convenience,
provided that Q will in fact be 1 for most instantiations of the test
class template.

Quote:
//option 2
template< class T , int Q> class test{ ... };
template< class T > class test<T, 1>{ ... };

With the option 2 the partial specialization declaration looks far more
normal. Therefore I would cherry pick the second declaration of option
2 along with the first declaration of option 1 - as the best of both
alternatives:

template< class T , int Q=1> class test { ... };
template< class T > class test<T, 1>{ ... };

This arrangement has the additional advantage that the partial
specialization is no longer dependent on the default parameter chosen
for Q. Otherwise, should Q's default parameter ever change, the partial
specialization would silently change along with it. And as every
programmer inevitably learns: an unanticipated side effect due to a
change in a program is never a welcome surprise.

Greg

---
[ 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.comeaucomputing.com/csc/faq.html ]
Back to top
cooleaf@gmail.com
Guest





PostPosted: Fri May 19, 2006 5:21 pm    Post subject: Re: partial template specialization Reply with quote



I think they are equivalent. in my option, the template class , the
compiler will make new class by it, and the new classes is same style.
so they are equivalent.

---
[ 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.comeaucomputing.com/csc/faq.html ]
Back to top
Gene Bushuyev
Guest





PostPosted: Fri May 19, 2006 6:21 pm    Post subject: Re: partial template specialization Reply with quote

"Bruno van Dooren" <bruno_nos_pam_van_dooren (AT) hotmail (DOT) com> wrote in message
news:NE3bg.433045$zE1.11530378 (AT) phobos (DOT) telenet-ops.be...
Quote:
Hi all,
I have a template class for which I want to provide a specialization.
there are 2 ways to do this, and I have the impression that they are 100%
equivalent.
So I was wondering: is there any difference, or perhaps one is preferred over
the other:

//option 1
template< class T , int Q=1> class test { ... };
template< class T > class test<T> {};

//option 2
template< class T , int Q> class test{ ... };
template< class T > class test<T, 1>{ ... };


The question is whether you need the default template parameter or not? The
first class can be used with single template parameter, e.g. test<int>, which is
handled by specialization; the second requires two parameters, e.g. test<int, 1>
corresponds to specialized template. That's the only difference between those
templates.
--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy

---
[ 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.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.