Member template function specialization in a template class

R

Ruben Campos

Greetings. Please, take a look to the next code, where I have a problem with
specialization of class member functions.

// ########## CPrinter.h ##########
#ifndef PRINTER_H
#define PRINTER_H

enum TPrintPolicy { POLICY1 = 0, POLICY2 };

template <typename T>
class CPrinter
{
public:
template <TPrintPolicy P> void Print (T const value);
};

#include "CPrinter.cpp"
#endif

// ########## CPrinter.cpp ##########
#include <iostream>
using std::cout;
using std::endl;

template <typename T>
template <TPrintPolicy P>
void
CPrinter <T>::print <P> (T const value)
{
cout << "CPrinter <T>::print <P> prints " << value << " value." << endl;
}

template <typename T>
template <>
void
CPrinter <T>::print <POLICY1> (T const value)
{
cout << "CPrinter <T>::print <POLICY1> prints " << value << " value." <<
endl;
}

// ########## MyClass.h ##########
#ifndef MY_CLASS_H
#define MY_CLASS_H

class MyClass
{
public:
void Foo ();
};

#endif

// ########## MyClass.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"

void
MyClass::Foo ()
{
CPrinter <int> printer;
printer.Print <POLICY2> (5);
}

// ########## main.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"

int
main (int argn, char ** argv)
{
CPrinter <int> printer;
printer.Print <POLICY1> (4);

MyClass x;
x.Foo();

return 0;
}

I've tried this with Microsoft Visual C++ .NET (7.x). If I comment the
CPrinter <T>::print <POLICY1> specialized implementation (in CPrinter.cpp),
this source builds and runs fine. However, compiler returns errors when
including that specialization of CPrinter <T>::print member function. As a
remarkable note, I've noticed that the number of errors returned by the
compiler increases when changing the order of the two #include in the
main.cpp file, with new "MyClass not defined" like errors.

Could someone say me what is happening here? Is there any right way to
specialize the template CPrinter <T>::print method?

Thank you vey much in advance.
 
R

rasbury

Ruben Campos said:
// ########## CPrinter.h ##########
#ifndef PRINTER_H
#define PRINTER_H

enum TPrintPolicy { POLICY1 = 0, POLICY2 };

template <typename T>
class CPrinter
{
public:
template <TPrintPolicy P> void Print (T const value);
};

#include "CPrinter.cpp"
#endif

// ########## CPrinter.cpp ##########
#include <iostream>
using std::cout;
using std::endl;

template <typename T>
template <TPrintPolicy P>
void
CPrinter <T>::print <P> (T const value)
{
cout << "CPrinter <T>::print <P> prints " << value << " value." <<
endl;
}

template <typename T>
template <>
void
CPrinter <T>::print <POLICY1> (T const value)
{
cout << "CPrinter <T>::print <POLICY1> prints " << value << " value."
<< endl;
}

// ########## MyClass.h ##########
#ifndef MY_CLASS_H
#define MY_CLASS_H

class MyClass
{
public:
void Foo ();
};

#endif

// ########## MyClass.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"

void
MyClass::Foo ()
{
CPrinter <int> printer;
printer.Print <POLICY2> (5);
}

// ########## main.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"

int
main (int argn, char ** argv)
{
CPrinter <int> printer;
printer.Print <POLICY1> (4);

MyClass x;
x.Foo();

return 0;
}

I've tried this with Microsoft Visual C++ .NET (7.x). If I comment the
CPrinter <T>::print <POLICY1> specialized implementation (in
CPrinter.cpp), this source builds and runs fine. However, compiler returns
errors when including that specialization of CPrinter <T>::print member
function. As a remarkable note, I've noticed that the number of errors
returned by the compiler increases when changing the order of the two
#include in the main.cpp file, with new "MyClass not defined" like errors.

Could someone say me what is happening here? Is there any right way to
specialize the template CPrinter <T>::print method?

One of the non-standards conforming issues with VC++ .NET is that it
requires template members to be defined within the template, inline style
(notes on standards conformance can be found in the documentation under
Visual C++ -> Visual C++ Reference -> C/C++ Languages -> C++ Language
Reference -> Standard Compliance Issues in Visual C++). Try moving the
definitions of the function and its specialisation into the class
definition. It is possible that the dependency of the errors on the order of
the #includes is to do with the compiler skipping the declaration of MyClass
in attempting to recover from errors raised in CPrinter.h (while compiling
main.cpp). HTH,

Richard Asbury
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top