Topic: Member Template Specialization not possible?


Author: Robert Spatschek <spatschk@physik.rwth-aachen.de>
Date: 2000/05/28
Raw View
Template Specialization is a very nice technique. A construction like
this appears quite often:

// ...
template <class T>
void f(T t) {...}

template <>
void f<int>(int t) {...}    // specialization for int
// ...

Everything is fine with this. It is also possible to define template
functions inside a class:

// ...
class A
{
  template <class T>
    void g(T t) {...}
};
// ...

My problems occur if I try to specialize this member function g. A code
fragment like

// ...
class A
{
  template <class T>
    void g(T t) {...}           // as before
  template <>
    void g<int>(int t) {...}    // int specialization
};
// ...

does not work on my computer. The compiler tells me that a
specialization is not allowed in this scope.
Is this a compiler-specific restriction (I am using gcc 2.95.2) or more
general? If the latter was true, why does the standard require this
limitation? Or am I simply doing something wrong?

Robert Spatschek.

---
[ 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: Martin Sebor <marts@att.net>
Date: 2000/05/29
Raw View
Robert Spatschek wrote:
>
....
>
> My problems occur if I try to specialize this member function g. A code
> fragment like
>
> // ...
> class A
> {
>   template <class T>
>     void g(T t) {...}           // as before
>   template <>
>     void g<int>(int t) {...}    // int specialization
> };
> // ...
>
> does not work on my computer. The compiler tells me that a
> specialization is not allowed in this scope.
> Is this a compiler-specific restriction (I am using gcc 2.95.2) or more
> general? If the latter was true, why does the standard require this
> limitation? Or am I simply doing something wrong?

This is an open core issue #44. If the proposed resolution is approved,
specializations of member templates will not be allowed to appear in the
class the're members of. They will need to be defined at namespace scope
like so:

struct A {
    template <class T>
    void g(T t) { }
};

template <>
void A::g<int>(int t) { }


--Martin

>
> Robert Spatschek.
>
> ---
> [ 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              ]

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