Topic: template specialization question


Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/07/01
Raw View
Terence Kelling wrote:
>
> I am trying to write a specialization for a class template which takes
> a type parameter for the case of another class template which takes a
> non-type parameter.  Is the following syntax correct, and is this
> considered to be a specialzation for foo or a partial specialization
> for foo?
>
> Thanks,
> Terence Kelling
>
> #include <iostream>
> using namespace std;
>
> template <int i>
> class bar { };
>
> template <class T>
> class foo {
>
>    public:
>       void print(void) const { cout << "generic" << endl; };
> };
>
> template<> template<int i> class foo<bar<i> > {
  ^^^^^^^^^^
  This is wrong; ommitting this leads to correct syntax:
  template<int i> class foo< bar<i> >
>
>    public:
>       void print(void) const { cout << "specialization: bar " << i << endl; };
> };

[...]
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/06/24
Raw View
I am trying to write a specialization for a class template which takes
a type parameter for the case of another class template which takes a
non-type parameter.  Is the following syntax correct, and is this
considered to be a specialzation for foo or a partial specialization
for foo?

Thanks,
Terence Kelling


#include <iostream>
using namespace std;

template <int i>
class bar { };


template <class T>
class foo {

   public:
      void print(void) const { cout << "generic" << endl; };
};


template<> template<int i> class foo<bar<i> > {

   public:
      void print(void) const { cout << "specialization: bar " << i << endl; };
};


void main(void) {

   foo<double> generic_foo;
   foo<bar<1> > specialized_foo1;
   foo<bar<2> > specialized_foo2;

   generic_foo.print();
   specialized_foo1.print();
   specialized_foo2.print();
};
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Mirek Fidler <cxl@usa.net>
Date: 1997/10/11
Raw View
Bill Gibbons wrote:

> Tomas Rylek <rylek@usa.net> wrote: (minor corrections added)
>
> >     it is not clear to mee, wheter following source is (will be)
> > ambiguous according to C++ draft:
> >
> > template <class T>
> > void Dump(Stream& s, const T& a) {  // #1
> >     s << a;
> > }
> >
> > template <class T>
> > class Vector {
> >     // some vector of T elements
> > };
> >
> > void Dump(Stream& s, const Vector<T>& v) {  // #2
> >     for(int i = 0; i < v.GetCount(); i++)
> >         s << v[i];
> > }
> >
> > void main() {
> >     Dump(cout, 45);
> >     Vector<int> v;
> >     v.Add(1); v.Add(2);
> >     // #3
> >     Dump(cout, v);// Uses specialization or is ambiguous ?
> >                   // Not clean in draft 2 as far as I have found
> > }
>
> Just to make sure we have the terminology clear...
>
>    #1 is a template
>    #2 is an ordinary non-template function
>

Oh, I ommited template <class T> line in #2, it should look like this:

template <class T>
void Dump(Stream& s, const Vector<T>& v) {  // #2
     for(int i = 0; i < v.GetCount(); i++)
         s << v[i];
 }

Now, what ? Either Dump is template. In my personal view, 'more'
specialised Dump should have precedence to 'less' one. But I am not
sure, what draft says. I think, that it is not legal according to draft.
But, pehaps, it should...

> Section 13.3.3 [over.match.best] of CD2 states that when a
> non-template
> function and a template specialization (implicit or explicit is
> irrelevant at this point) are equally good matches, the non-template
> function is preferred.  So #2 is called in the example.

Without 'template <class T>'  #2 is ill-formed anyway, isnt it ?

Mirek
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: bill@gibbons.org (Bill Gibbons)
Date: 1997/10/14
Raw View
In article <343DF857.D7197C07@usa.net>, Mirek Fidler <cxl@usa.net> wrote:

 > > > template <class T>
 > > > void Dump(Stream& s, const T& a) {  // #1
 > > >     s << a;
 > > > }
 > > >
 > > > template <class T>
 > > > class Vector {
 > > >     // some vector of T elements
 > > > };
 > > >
 > > > template <class T>
 > > > void Dump(Stream& s, const Vector<T>& v) {  // #2
 > > >     for(int i = 0; i < v.GetCount(); i++)
 > > >         s << v[i];
 > > > }
 > In my personal view, 'more'
 > specialised Dump should have precedence to 'less' one. But I am not
 > sure, what draft says. I think, that it is not legal according to draft.
 > But, pehaps, it should...

Yes, #2 is more specialized and should be preferred.  Not many
compilers implement this yet.

This has sometimes been called "partial specialization of function
templates", but in the (almost) final standard the term
"partial specialization" is used for class template only.

So #2 is "more specialized according to the partial ordering rules
for function templates".


-- Bill Gibbons
   bill@gibbons.org
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/10/15
Raw View
Bill Gibbons wrote:
 >
 > In article <343DF857.D7197C07@usa.net>, Mirek Fidler <cxl@usa.net> wrote:
 >
 >  > > > template <class T>
 >  > > > void Dump(Stream& s, const T& a) {  // #1

[...]

 >  > > >
 >  > > > template <class T>
 >  > > > class Vector {
 >  > > >     // some vector of T elements
 >  > > > };
 >  > > >
 >  > > > template <class T>
 >  > > > void Dump(Stream& s, const Vector<T>& v) {  // #2

[...]

 > Yes, #2 is more specialized and should be preferred.  Not many
 > compilers implement this yet.
 >
 > This has sometimes been called "partial specialization of function
 > templates", but in the (almost) final standard the term
 > "partial specialization" is used for class template only.

Correct, but I'll write that more explicitly:

The two Dump functions are unrelated overloaded functions. The one
which is called is selected by the partial specialisation rules.

Two class templates with the same name aren't overloaded (only
functions can be overloaded), they are particular cases of a
general one (they aren't unrelated); they are called
specialisations of a class template.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Tomas Rylek <rylek@usa.net>
Date: 1997/10/08
Raw View
Hi,
    it is not clear to mee, wheter following source is (will be)
ambiguous according to C++ draft:

template <class T>
void Dump(Stream& s, const T& a) {
    s << a;
}

template <class T>
class Vector {
    // some vector of T elements
};

void Dump(Stream& s, const Vector<T>& v) {
    for(int i = 0; i < v.GetCount(); i++)
        s << v[i];
}

void main() {
    Dump(45);
    Vector<int> v;
    v.Add(1); v.Add(2);
    Dump(v);// Uses specialization or is ambiguous ? Not clean in draft
2 as far as I have found
}

Mirek
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]