1
votes

I have a problem concerning partial class template specialization in c++. I have one class A with two templates T,U. Then i want two class specializations with exclusive methods print_me(), only visible for those two specializations. Apart from that i need fully specialized member functions add_integer/add_double. When i implement those methods in the header file, it compiles fine. But id really like my implementations to be in the source file, which is no problem for the fully specialized functions, but for the partial ones. I get following errors for the code below:

test.h:27:25: error: declaration of ‘void A<int, U>::print_me()’ outside of class is not definition [-fpermissive]
test.h:30:28: error: declaration of ‘void A<double, U>::print_me()’ outside of class is not definition [-fpermissive]

But if i leave out the declarations of print_me() in the header file i get an undefined reference error, which i understand, as my compiler cant know, which methods to instantiate when compiling.

test.h:

//Primary Template Class
template <typename T, typename U>
class A{};

//Partial Specialization 1
template <typename U>
class A<int,U>
{
public:
void add_double(double a);
void print_me();
};

//Partial Specialization 2
template <typename U>
class A<double,U>
{
public:
void add_int(int a);
void print_me();
};

//explicit specialization
template <>
void A<int,double>::add_double(double a);

//explicit specialization
template <>
void A<double,int>::add_int(int a);

//partial specialization
template <typename U>
void A<int,U>::print_me();

//partial specialization
template <typename U>
void A<double,U>::print_me();

test.cpp

#include "test.h"

template <>
void A<int,double>::add_double(double a){}

template <>
void A<double,int>::add_int(int a){}


template <typename U>
void A<int,U>::print_me(){};

template <typename U>
void A<double,U>::print_me(){};

for testing purpose main.cpp

#include "test.h"

int main()
{
A<int,double> * a_0=new A<int,double>;
A<double,int> * a_1=new A<double,int>;

a_0->add_double(1.1);
a_0->print_me();

a_1->add_int(1);
a_1->print_me();

return 0;
}

So is there any possibility to have the print_me() methods implemented in the source file? Whats the difference between fully and partially specialized member functions in this context?

Any hints will be appreciated

1
The difference is that a partial specialisation is still a template, a (full) specialisation is not.Angew is no longer proud of SO
Thanks i can see that clear now. But how do i handle this template?! Is it possible to implement print_me() in the cpp file?Peter Power

1 Answers

1
votes

as @Angew mentioned, partial specialization is still a template, which is just a blueprint for yourprint_memethod. you need to instantiate your template function. for that you can use explicit instantiation or implicit instantiation in your test.cpp file.

Explicit Instantiation:

template
void A<double,int>::add_int(int a);

Implicit Instantiation:
just define a dummy function in test.cpp which will call the fully specialized template function print me (you don't have to use this dummy function) :

void instatiatePrintMe()
{ 
    A<int, double> a;
    a.print_me();
}

you can read here for more information: C++ - Defining class template (header/source file)