About partially specify template member function

P

PengYu.UT

I have the following two program. The first one compiles well, but the
second one doesn't. The only difference between them is one more
template parameter is added for the second program.

Would you please help on how to make the second program work?

Thanks,
Peng

/************first************************/
#include <iostream>

class tag1;
class tag2;

template <typename T>
class A{
public:
void print();
};

template <>
void A<tag1>::print() {
std::cout << "tag1" << std::endl;
}

template <>
void A<tag2>::print() {
std::cout << "tag2" << std::endl;
}

int main(int ac, char* av[])
{
A<tag1> a1;
a1.print();

A<tag2> a2;
a2.print();
}
/************end first************************/

/**************second*************************/
#include <iostream>

class tag1;
class tag2;

template <typename T1, typename T>
class A{
public:
void print();
};

template <typename T1>
void A<T1, tag1>::print() {
std::cout << "tag1" << std::endl;
}

template <typename T1>
void A<T1, tag2>::print() {
std::cout << "tag2" << std::endl;
}

int main(int ac, char* av[])
{
A<int, tag1> a1;
a1.print();

A<int, tag2> a2;
a2.print();
}
/**************end second*************************/
 
R

Rolf Magnus

I have the following two program. The first one compiles well, but the
second one doesn't. The only difference between them is one more
template parameter is added for the second program.

Would you please help on how to make the second program work?

You need to specialize the whole class, not just the function. And your
syntax for partial specialization is not correct. See below.
#include <iostream>

class tag1;
class tag2;

template <typename T1, typename T>
class A{
public:
void print();
};

template <>
template<typename T1>
class A<T1, tag1> {
public:
void print();
};

template said:
template <typename T1>
void A<T1, tag1>::print() {
std::cout << "tag1" << std::endl;
}

template <>
template<typename T1>
class A<T1, tag2> {
public:
void print();
};

template said:
template <typename T1>
void A<T1, tag2>::print() {
std::cout << "tag2" << std::endl;
}

int main(int ac, char* av[])
{
A<int, tag1> a1;
a1.print();

A<int, tag2> a2;
a2.print();
}
 
P

PengYu.UT

#include said:
class tag1;
class tag2;

template <typename T1, typename T>
class A{
public:
void print();
};

template <>
template<typename T1>
class A<T1, tag1> {
public:
void print();
};

template said:
template <typename T1>
void A<T1, tag1>::print() {
std::cout << "tag1" << std::endl;
}

template <>
template<typename T1>
class A<T1, tag2> {
public:
void print();
};

template said:
template <typename T1>
void A<T1, tag2>::print() {
std::cout << "tag2" << std::endl;
}

int main(int ac, char* av[])
{
A<int, tag1> a1;
a1.print();

A<int, tag2> a2;
a2.print();
}

The above program works. But I have to define
class A<T1, tag1> and class A<T1, tag2> even if they exactly the same
as class A<T1, T>. This might introduce a lot of redundances if the
class A is a very big class and only the "print" functions are slightly
different for different template instantiations.

Is there any walkaround?
 
R

Rolf Magnus

#include <iostream>

class tag1;
class tag2;

template <typename T1, typename T>
class A{
public:
void print();
};

template <>
template<typename T1>
class A<T1, tag1> {
public:
void print();
};

template said:
template <typename T1>
void A<T1, tag1>::print() {
std::cout << "tag1" << std::endl;
}

template <>
template<typename T1>
class A<T1, tag2> {
public:
void print();
};

template said:
template <typename T1>
void A<T1, tag2>::print() {
std::cout << "tag2" << std::endl;
}

int main(int ac, char* av[])
{
A<int, tag1> a1;
a1.print();

A<int, tag2> a2;
a2.print();
}

The above program works. But I have to define
class A<T1, tag1> and class A<T1, tag2> even if they exactly the same
as class A<T1, T>. This might introduce a lot of redundances if the
class A is a very big class and only the "print" functions are slightly
different for different template instantiations.

Is there any walkaround?

Put the actual code in a non-member function that is called by print. Then
you can partially specialize that function.
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top