By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Member template function specialization in a template class

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Ruben Campos" <Ru**********@robotica.uv.es> wrote in message
news:cu**********@peque.uv.es...
// ########## 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
Jul 23 '05 #2

P: n/a
rasbury wrote:
[redacted]


That's only in VC.NET 2002 (7.0). 7.1's docs say that it's been fixed.

Jul 23 '05 #3

P: n/a
> template <typename T>
template <>
void
CPrinter <T>::Print <POLICY1> (T const value)
{
cout << "CPrinter <T>::Print <POLICY1> prints " << value << " value." << endl;
}


The standard says that you have to fully specialize an enclosing
template before an enclosed one. But there are workarounds:
http://groups.google.se/groups?hl=sv...25C3%25B6kning

Daniel

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.