Dear all,

In the discussion of 23-06-2003 it was concluded that the following
concept compiles under VC++.NET 2003:

class KA
{
public:
template <typename U>
void Foo(const U& rU);
};

template <typename U>
void KA::Foo(const U& rU)
{
}

template <>
void KA::Foo(const std::string& rstr)
{
}

unfortunately if the class is also a template, an out of inline
definition does NOT compile, even under the new improved VC++.NET 2003
compiler:

template <typename T>
class KA_T
{
public:
template <typename U>
void Foo(const U& rU);
};

template <typename T>
template <typename U>
void KA_T<T>::Foo(const U& rU)
{
}

template <typename T>
template <>
void KA_T<T>::Foo(const std::string& rstr)
{
}

this gives error C2244. Of course there are many ways to circumvent
this,
but my main question is this legal c++ or do I something wrong?

Me.

Re: Explicit Specialization of Member Templates by Vincent

Vincent
Mon Aug 18 11:26:25 CDT 2003

Programmer_blabla wrote:
> Dear all,
>
> In the discussion of 23-06-2003 it was concluded that the following
> concept compiles under VC++.NET 2003:
>
> class KA
> {
> public:
> template <typename U>
> void Foo(const U& rU);
> };
>
> template <typename U>
> void KA::Foo(const U& rU)
> {
> }
>
> template <>
> void KA::Foo(const std::string& rstr)
> {
> }
>
> unfortunately if the class is also a template, an out of inline
> definition does NOT compile, even under the new improved VC++.NET 2003
> compiler:
>
> template <typename T>
> class KA_T
> {
> public:
> template <typename U>
> void Foo(const U& rU);
> };
>
> template <typename T>
> template <typename U>
> void KA_T<T>::Foo(const U& rU)
> {
> }
>
> template <typename T>
> template <>
> void KA_T<T>::Foo(const std::string& rstr)
> {
> }
>
> this gives error C2244. Of course there are many ways to circumvent
> this,
> but my main question is this legal c++ or do I something wrong?

According to my reading of "C++ Templates (The Complete Guide)" it is
perfectly legal
And since moving the functions inside the class definition makes it
compile (even on VC6) I would be surprised if the code was at fault


Re: Explicit Specialization of Member Templates by Igor

Igor
Mon Aug 18 11:30:35 CDT 2003

"Programmer_blabla" <programmer_blabla@hotmail.com> wrote in message
news:e3e73a80.0308180749.4326fd80@posting.google.com...
> template <typename T>
> class KA_T
> {
> public:
> template <typename U>
> void Foo(const U& rU);
> };
>
> template <typename T>
> template <typename U>
> void KA_T<T>::Foo(const U& rU)
> {
> }
>
> template <typename T>
> template <>
> void KA_T<T>::Foo(const std::string& rstr)
> {
> }
>
> this gives error C2244. Of course there are many ways to circumvent
> this,
> but my main question is this legal c++ or do I something wrong?

It is illegal under C++ standard 14.7.3/18:

... the declaration shall not explicitly specialize a class member
template if its enclosing class templates are not explicitly specialized
as well.

--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Re: Explicit Specialization of Member Templates by Doug

Doug
Mon Aug 18 12:03:03 CDT 2003

Igor Tandetnik wrote:

>"Programmer_blabla" <programmer_blabla@hotmail.com> wrote in message
>news:e3e73a80.0308180749.4326fd80@posting.google.com...
>> template <typename T>
>> class KA_T
>> {
>> public:
>> template <typename U>
>> void Foo(const U& rU);
>> };
>>
>> template <typename T>
>> template <typename U>
>> void KA_T<T>::Foo(const U& rU)
>> {
>> }
>>
>> template <typename T>
>> template <>
>> void KA_T<T>::Foo(const std::string& rstr)
>> {
>> }
>>
>> this gives error C2244. Of course there are many ways to circumvent
>> this,
>> but my main question is this legal c++ or do I something wrong?
>
>It is illegal under C++ standard 14.7.3/18:
>
>... the declaration shall not explicitly specialize a class member
>template if its enclosing class templates are not explicitly specialized
>as well.

But Foo is a function, not a class template. As Vincent noted in his
follow-up message, VC does allow explicit specialization of member template
functions, provided you define them in the class body. According to Comeau,
even this is illegal. (The VC header <comip.h> is going to hate to hear
about this!) Maybe if you've got a spare couple of days, you can figure out
the set of things the standard may say about this. :)

--
Doug Harrison
Microsoft MVP - Visual C++

Re: Explicit Specialization of Member Templates by Aaron

Aaron
Mon Aug 18 12:27:54 CDT 2003

You can get it to compile like so:

template <typename T>
class KA_T
{
public:
template <typename U>
void Foo(const U& rU);
void Foo(const std::string& rstr); // ADD THIS LINE
};

template <typename T>
template <typename U>
void KA_T<T>::Foo(const U& rU)
{
}

template <typename T>
// template <> // REMOVE THIS LINE
void KA_T<T>::Foo(const std::string& rstr)
{
}

Regards,
Aaron Queenan.

"Programmer_blabla" <programmer_blabla@hotmail.com> wrote in message
news:e3e73a80.0308180749.4326fd80@posting.google.com...
> Dear all,
>
> In the discussion of 23-06-2003 it was concluded that the following
> concept compiles under VC++.NET 2003:
>
> class KA
> {
> public:
> template <typename U>
> void Foo(const U& rU);
> };
>
> template <typename U>
> void KA::Foo(const U& rU)
> {
> }
>
> template <>
> void KA::Foo(const std::string& rstr)
> {
> }
>
> unfortunately if the class is also a template, an out of inline
> definition does NOT compile, even under the new improved VC++.NET 2003
> compiler:
>
> template <typename T>
> class KA_T
> {
> public:
> template <typename U>
> void Foo(const U& rU);
> };
>
> template <typename T>
> template <typename U>
> void KA_T<T>::Foo(const U& rU)
> {
> }
>
> template <typename T>
> template <>
> void KA_T<T>::Foo(const std::string& rstr)
> {
> }
>
> this gives error C2244. Of course there are many ways to circumvent
> this,
> but my main question is this legal c++ or do I something wrong?
>
> Me.



Re: Explicit Specialization of Member Templates by programmer_blabla

programmer_blabla
Tue Aug 19 08:25:59 CDT 2003

My answer somehow ended up in an new thread. see "Explicit
Specialization of Member Templates 2".

Me