Topic: partial template specialization


Author: "cooleaf@gmail.com" <cooleaf@gmail.com>
Date: Fri, 19 May 2006 11:48:37 CST
Raw View
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                      ]





Author: "Gene Bushuyev" <spam@spamguard.com>
Date: Fri, 19 May 2006 12:38:16 CST
Raw View
"Bruno van Dooren" <bruno_nos_pam_van_dooren@hotmail.com> wrote in message
news:NE3bg.433045$zE1.11530378@phobos.telenet-ops.be...
> 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                      ]





Author: "Bruno van Dooren" <bruno_nos_pam_van_dooren@hotmail.com>
Date: Thu, 18 May 2006 14:57:50 CST
Raw View
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@hotmail.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                      ]





Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Fri, 19 May 2006 06:40:38 CST
Raw View
Bruno van Dooren wrote:
> 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.

> //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                      ]