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*************************/ 3 1418 Pe*******@gmail.com wrote: 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<> 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<> 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(); }
> > #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<> 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<> 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? Pe*******@gmail.com wrote: > #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<> > 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<> > 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: CoolPint |
last post by:
As a self-exercise, I am trying to write a generic Priority Queue,
which would store any type and and accept any user-definable
"priority" function.
After much tinkering, I came up with...
|
by: Martin Magnusson |
last post by:
I have a partially specialized templated class, which at one point needs
to pass pointers to some of its methods to another function. However, it
seems that my compiler (gcc) and me don't quite...
|
by: mrstephengross |
last post by:
Ok, I've got a class with two template parameters (A and B), and a
member function foo(). I want to specialize foo for a particular class
A. Is this possible? The following code shows an example:
...
|
by: amparikh |
last post by:
I have something like this.
typedef enum TYPES{ X =0, Y,
Z,
MAX};
template <typename T>
class A
{
public:
|
by: James Aguilar |
last post by:
Guys,
When I specialize a template class member function (I.e. a member
function of a template class) based on that class' type, bad things
happen. Here's some code:
---- test_header.h...
|
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...
|
by: Rahul |
last post by:
Hi,
Is there a way to partially specialize only a member function of a
template class (not the whole class).
e.g.
template <typename A, typename B>
class Base
{
public:
|
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:
|
by: mathieu |
last post by:
Hi there,
I know this is not possible in c++. So my question, how should I
rewrite the following piece of code (without using a dummy class which
template parameter could be use for partial...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |