Member template function specialization in a template class



 DEVELOP > c-Plus-Plus > Member template function specialization in a template class

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Ruben Campos"
Date: 14 Feb 2005 06:32:39 AM
Object: Member template function specialization in a template class
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.
.

User: "rasbury"

Title: Re: Member template function specialization in a template class 14 Feb 2005 01:39:55 PM
"Ruben Campos" <Ruben.Campos@robotica.uv.es> wrote in message
news:cuq5ou$nlu$1@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
.
User: "red floyd"

Title: Re: Member template function specialization in a template class 14 Feb 2005 02:33:48 PM
rasbury wrote:

[redacted]

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


User: ""

Title: Re: Member template function specialization in a template class 15 Feb 2005 04:40:36 AM

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&lr=&threadm=opslgjc0jbti5cme%40devlx007.ipcb.net&rnum=2&prev=/groups%3Fq%3DEnclosing%2Bdo_foo%26hl%3Dsv%26btnG%3DGoogle-s%25C3%25B6kning
Daniel
.


  Page 1 of 1

1

 


Related Articles
Full specialization of a member function template of a class template
Specialization of member function template in template class?
Template class member function specialization
unexpected compile error w/ member function specialization of a template class
template class member function specialization
address-of a template class template member function
static member function in class template
Query: How to declare a template class member function 'extern "C"' ?
Specializing Member Function Template of Class Template?
Specialising a template member function of a template class?
Question Can I use explicit qualifier after member function name of a template class?
recursiveness on template member function of a template class
Trouble specializing a member function in a template class
typedef'd return type for a template class member function
problem with inner class of a class template as member function returntype
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER