 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
bigfaceworm Guest
|
Posted: Wed Jul 28, 2004 3:09 am Post subject: template class with template member function, want to specia |
|
|
All,
I'm trying what I would have expected to be a relatively
straight-forward problem.
I've got a class, I want to add a new member function that's a
templatized member function. So far so good.
Now I want to specialize that template member function. But I can't
get past the compile time errors.
Is this possible? Or am I just dreaming.
I'm building on Linux with gcc 3.2.2
#include <iostream>
using namespace std;
template <class T>
struct Mine
{
Mine(const T&t) : t_(t) {} ;
T t_;
template <bool>
void func(ostream &o);
};
template <class T>
template <bool b>
void Mine<T>::func(ostream &o)
{
o << "Func for type T " << t_ << endl;
}
template
template <bool>
void Mine<T>::func<false>(ostream &o) ///// COMPILE ERROR HERE
{
o << "FALSE FALSE FALSE FALSE Func for type T " << t_ << endl;
}
int main(int, char *[])
{
Mine
t.func<true>(cout);
t.func<false>(cout);
return 0;
}
//////////////////////
here are the errors:
~/tmp/template_specialize.cpp:25: template-id `func<false>' in
declaration of primary template
~/tmp/template_specialize.cpp:25: redefinition of `void
Mine<T>::func(_STL::ostream&)'
~/tmp/template_specialize.cpp:18: `void Mine<T>::func(_STL::ostream&)'
previously declared here
~/tmp/template_specialize.cpp:25: no `void
Mine<T>::func(_STL::ostream&)' member function declared in class
`Mine<T>'
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Jul 28, 2004 3:21 pm Post subject: Re: template class with template member function, want to sp |
|
|
bigfaceworm wrote:
| Quote: | I'm trying what I would have expected to be a relatively
straight-forward problem.
I've got a class, I want to add a new member function that's a
templatized member function. So far so good.
Now I want to specialize that template member function. But I can't
get past the compile time errors.
Is this possible? Or am I just dreaming.
|
It is not possible. You're not allowed to specialise a member template
without specialising the class. See Standard 14.7.3/18.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gareth Stockwell Guest
|
Posted: Thu Jul 29, 2004 10:30 am Post subject: Re: template class with template member function, want to sp |
|
|
[email]bigfaceworm (AT) hotmail (DOT) com[/email] (bigfaceworm) wrote in message news:<2bd747c7.0407271541.702d8eb7 (AT) posting (DOT) google.com>...
| Quote: | I've got a class, I want to add a new member function that's a
templatized member function. So far so good.
Now I want to specialize that template member function. But I can't
get past the compile time errors.
Is this possible? Or am I just dreaming.
|
Well, not directly, but you can *almost* achieve this with a
work-around. The idea is that you declare a structure outside of your
class, which is templated on all the parameters involved (i.e. all
your class parameters, plus the parameters for the member function).
Inside this new structure, define a static member function which takes
all the arguments of the original member function, plus a reference to
the original object. This function constitutes the implementation for
your original templated member function.
An example should make this clear:
//============================================
#include <iostream>
// Forward declaration of our class
template<typename T> struct MyClass;
// Implementation for the non-specialised version of MyClass::func
template<typename T, bool B>
struct FuncImplementation {
static void execute(const MyClass<T>& c, std::ostream& out)
{ out << "non-specialised " << c.t() << std::endl; }
};
// Specialised version
template
struct FuncImplementation<T,false> {
static void execute(const MyClass<T>& c, std::ostream& out)
{ out << "specialised " << c.t() << std::endl; }
};
// The class itself
template
class MyClass {
T t_;
public:
MyClass(const T& t) : t_(t) { }
// Member function defers to the static method of
// the implementation class defined above
template<bool B>
void func(std::ostream& out) const
{ FuncImplementation<T,B>::execute(*this, out); }
// Accessor for the data member
const T& t() const { return t_; }
};
int main() {
MyClass<int> C(10);
C.func<true>(std::cout);
C.func<false>(std::cout);
}
//============================================
The reason it's only *almost* a solution is that in the general case,
FuncImplementation::execute cannot truly emulate the original member
function, because it does not have access to the private members of
MyClass. Friendship? Unless I'm missing a trick, you can't declare a
partial specialisation to be a friend...
HTH
Gareth
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|