473,221 Members | 1,932 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,221 software developers and data experts.

Trouble specializing a member function in a template class

/* --------------------------------------------------------------------------
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>::print() 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>::print() const
{ cout << "Output: " << a_ << ", " << b_ << endl; }
// Full specialization of print() for <string, char> types
template<> void C<string, char>::print() 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>::print() const' member function
declared in class `C<A, int>'
// a.cpp:33: template definition of non-template `void C<A,
int>::print() 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>::print() 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;
}
Jul 22 '05 #1
2 5734
Jeff wrote in news:7b**************************@posting.google.c om:
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.

Your problem is that you are trying to create a partial specialization
of a function (non-static member in this case, but that doesn't matter).
For some reason this is not allowed by the language (possibly the
standards committee thought overloading was enough). The workaround
as always is to add another layey of indirection (see 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>::print() specialization works.
Yup explicit specialization's (template <>) are allowed.


#include <iostream>
#include <typeinfo>

struct example
{
template < typename U, typename V >
void patialy_specialized( U const &u, V const &v );

};

/* helper class
*/
template < typename U, typename V >
struct example_patialy_specialized
{
static void apply( example * that, U const &u, V const &v )
{
std::cerr
<< "default version "
<< typeid( U ).name()
<< " - "
<< typeid( V ).name()
<< "\n"
;
}
};

/* defenition of template method, indirects through the
"helper" above or (importantly) one of its specializations.
*/
template < typename U, typename V >
inline void example::patialy_specialized( U const &u, V const &v)
{
return example_patialy_specialized< U, V >::apply( this, u, v );
}

/* demo specialization of the "helper"
*/
template < typename U >
struct example_patialy_specialized< U, int >
{
static void apply( example * that, U const &arg, int )
{
std::cerr << "U int version " << typeid( U ).name() << "\n";
}
};

int main()
{
example ex;
double d = 1;

ex.patialy_specialized( d, d ); // default
ex.patialy_specialized( d, 1 ); // partial specialization
}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
Thanks Rob, got it. I guess I'd need to make the
example_patialy_specialized<U, V> template a friend inside the
example<U, V> template to access any of the private member data. Kind
of unfortunate, it clutters up what seems to be a fairly simple
problem (mind you, I have no need for it yet, just testing)... but
that's templates for you.

Thanks again, a big help.
Jeff
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Drummond | last post by:
Hello, Is there any way of specializing a templated function on a type which is itself templated? Here's the simplest example of the problem I can think of. Say I have written an implementation...
9
by: Eric Wang | last post by:
VC 7.1.3088 Bug: When a class defines a template member function taking (T const &), then an explicit specialization taking a non-const & argument (U) causes a C1001 at msc1.cpp line 2701. The...
4
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...
2
by: Simon G Best | last post by:
Hello! I'm trying to specialize a member function template of a class template, like this:- template<typename Tclass thingy { public: template<typename UT f (const U &) const; };
6
by: Howard | last post by:
Hi, I have a function in three unrelated but similar classes. The code in the member functions is identical for all three classes. What I want is to make a template which defines the function,...
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...
8
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:
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:
2
by: subramanian100in | last post by:
consider the following program #include <iostream> using namespace std; class Rec { public: Rec(int arg = 10) : val(arg) { }
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.