J
Jeff
/* --------------------------------------------------------------------------
Hello,
I was experimenting with class templates and specializing member
functions and came across a simple problem which I can't figure out.
I have a simple class C with 2 template member objects, and a function
print() that prints the value of these objects. I defined a
specialization of print() for C<string, char> which is called
correctly. Then I wanted to define a specialization of print() that
would be called for C<class T, int>, where T is any type. I couldn't
get this code to compile, let alone run, and tried various ways to
make it work. The full source is below.
Is there a nice solution to this problem? I read somewhere (can't
recall where) that you can't specialize member functions for classes
without specializing the whole class, and yet the C<string,
char>:rint() specialization works.
Jeff
---------------------------------------------------------------------------
*/
#include <iostream>
#include <string>
using namespace std;
template<class A, class B>
class C {
public:
C(A a, B b) : a_(a), b_(b) {}
A a_;
B b_;
void print() const;
};
// Template definition of print()
template<class A, class B> void C<A, B>:rint() const
{ cout << "Output: " << a_ << ", " << b_ << endl; }
// Full specialization of print() for <string, char> types
template<> void C<string, char>:rint() const {
cout << "C<string, char> output: " << a_ << " ... " << b_ << endl;
}
// Trouble here ---------------------------------------------
// the following partial specialization gives the compiler error:
// a.cpp:33: no `void C<A, int>:rint() const' member function
declared in class `C<A, int>'
// a.cpp:33: template definition of non-template `void C<A,
int>:rint() const'
// (using GCC 3.2)
// if B is type int, call this...
//template<class A, int> //doesn't work
//template<class A, class B> // doesn't work
template<class A> // doesn't work
void
C<A, int>:rint() const {
cout << "C<A, int> output: ";
for (int i=0; i<b_; ++i) { cout << a_ << endl; }
cout << endl;
}
// end of Trouble -------------------------------------------
int main() {
// calls template definition, prints "Output: a, 1.23456"
C<char, float> myC('a', 1.23456);
myC.print();
cout << endl;
// calls full specialization successfully, prints "C<string, char>
output: hello there ... b"
C<string, char> C2("hello there", 'z');
C2.print();
cout << endl;
// should call partial specialization, want it to print "C<A, int>
output: boo!boo!boo!boo!boo!"
C<string, int> pc("boo!", 5);
pc.print();
cout << endl;
return 0;
}
Hello,
I was experimenting with class templates and specializing member
functions and came across a simple problem which I can't figure out.
I have a simple class C with 2 template member objects, and a function
print() that prints the value of these objects. I defined a
specialization of print() for C<string, char> which is called
correctly. Then I wanted to define a specialization of print() that
would be called for C<class T, int>, where T is any type. I couldn't
get this code to compile, let alone run, and tried various ways to
make it work. The full source is below.
Is there a nice solution to this problem? I read somewhere (can't
recall where) that you can't specialize member functions for classes
without specializing the whole class, and yet the C<string,
char>:rint() specialization works.
Jeff
---------------------------------------------------------------------------
*/
#include <iostream>
#include <string>
using namespace std;
template<class A, class B>
class C {
public:
C(A a, B b) : a_(a), b_(b) {}
A a_;
B b_;
void print() const;
};
// Template definition of print()
template<class A, class B> void C<A, B>:rint() const
{ cout << "Output: " << a_ << ", " << b_ << endl; }
// Full specialization of print() for <string, char> types
template<> void C<string, char>:rint() const {
cout << "C<string, char> output: " << a_ << " ... " << b_ << endl;
}
// Trouble here ---------------------------------------------
// the following partial specialization gives the compiler error:
// a.cpp:33: no `void C<A, int>:rint() const' member function
declared in class `C<A, int>'
// a.cpp:33: template definition of non-template `void C<A,
int>:rint() const'
// (using GCC 3.2)
// if B is type int, call this...
//template<class A, int> //doesn't work
//template<class A, class B> // doesn't work
template<class A> // doesn't work
void
C<A, int>:rint() const {
cout << "C<A, int> output: ";
for (int i=0; i<b_; ++i) { cout << a_ << endl; }
cout << endl;
}
// end of Trouble -------------------------------------------
int main() {
// calls template definition, prints "Output: a, 1.23456"
C<char, float> myC('a', 1.23456);
myC.print();
cout << endl;
// calls full specialization successfully, prints "C<string, char>
output: hello there ... b"
C<string, char> C2("hello there", 'z');
C2.print();
cout << endl;
// should call partial specialization, want it to print "C<A, int>
output: boo!boo!boo!boo!boo!"
C<string, int> pc("boo!", 5);
pc.print();
cout << endl;
return 0;
}