Class Template Specialization in MS-Visual

giligen
giligen used Ask the Experts™
on
Help!!!

I've got a code which I wrote for VxWorks that includes class template specialization. When being compiled/used with VxWorks it works PERFECT.

Being very naieve I've tried porting the same code to Windows (MS-Visual C++ 6.0), only this time it doesn't compile....

Here's the code:
// General class to be used by all types
template<typename T>
class NullEqOp
{
public:
     const T& operator() (const T& elem) {
          return elem;
     }
};
// Specialized class to be used with pointer types
template<typename T>
class NullEqOp<T*>
{
public:
     const T& operator() (const T * const & elem) {
          return *elem;
     }
};

Here's the output:
error C2989: '?$NullEqOp@PAVtemplate-parameter-BAB@' : template class has already been defined as a non-template class
error C2988: unrecognizable template declaration/definition

Is there another syntax which Microsoft supports?
BTW, I believe that my code is compliant with the standard....

Thanks in advance,

-- Giligen
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Checking the internet it seems that MS VC6 does not support it?!

Is there a service pack or anything I could do?

Author

Commented:
Checking the internet it seems that MS VC6 does not support it?!

Is there a service pack or anything I could do?
jkr
Top Expert 2012

Commented:
It simply does not work with VC++ 6.0 - see http://support.microsoft.com/default.aspx?scid=KB;en-us;240866& ("BUG: ERROR C2989 and C2988 on Class Template Partial Specializations")

SYMPTOMS
You may get the following error message if you try to use class template partial specializations:

error C2989: 'A<T,2>' : template class has already been defined as a non-template class error C2988: unrecognizable template declaration/definition
Please refer to the sample in the More Information section for details.

CAUSE
The compiler does not support class template partial specializations as specified in the C++ Standard (14.5.4.1) quoted here:

>>Is there a service pack or anything I could do?

The "Service Pack" is called MSVC++ 7.0 :o)
1- A primary class template declaration is one in which the class template name is an identifier. A template declaration in which the class template name is a template-id, is a partial specialization of the class template named in the template-id. A partial specialization of a class template provides an alternative definition of the template that is used instead of the primary definition when the arguments in a specialization match those given in the partial specialization (temp.class.spec.match). The primary template shall be declared before any specializations of that template. If a template is partially specialized then that partial specialization shall be declared before the first use of that partial specialization that would cause an implicit instantiation to take place, in every translation unit in which such a use occurs; no diagnostic is required.

Please refer to the C++ Standard section 14.5.4 for more information on this.
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Author

Commented:
Have you heard about any problems regarding template usage in MS-VC 7.0?

Commented:
I have heard a friend to a friend that have tried it with 7.0 and got disappointed. The template partial spec. didn't work as it supposed to in every case.
Top Expert 2012
Commented:
I mainly use VC6, but just gave it a try with VC7 - the result is a 'undeclared identifier 'T'' at the line

class NullEqOp<T*>

:-(

Author

Commented:
MS sucks.
Thanks.

Commented:
No, MS sucks big time.

Author

Commented:
Can't argue with that... :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial