473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.
Jul 23 '05 #1
3 1669
"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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short,...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
9
by: Ian | last post by:
Can it be done? If so, what's the syntax. For example a full specialisation, template <typename T> struct X { template <typename C> void foo( C a ) {} };
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
13
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm...
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.