Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

virtual member template functions workaround

0 views
Skip to first unread message

Matthieu Paindavoine

unread,
Aug 27, 2003, 5:39:14 AM8/27/03
to
Hello,

I am coding a < composite / visitor / combinator > sandwich, and I
bumped again what seems a tough cookie: virtual member template
functions are not allowed.

#include <iostream>

class A
{
public:
template < class T >
void Op(T& _t)
{
std::cout << "A::Op " << _t.vis() << std::endl;
}

virtual void V_Op()
{
std::cout << "A::V_Op" << std::endl;
}
};

class B : public A
{
public:
template < class T >
void Op(T& _t)
{
std::cout << "B::Op " << _t.vis() << std::endl;
}

virtual void V_Op()
{
std::cout << "B::V_Op" << std::endl;
}
};

class X
{
public:
void vis()
{
std::cout << "X::vis" << std::endl;
}
};

int main()
{
A* a = new A();
A* b = new B();
X x;
a->Op(x);
a->V_Op();
b->Op(x);
b->V_Op();
}

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
So I would love to stick a virtual in front of the void Op(T& _t), but
I can't

Is there a workaround, or this is dead-end an indication I took a
wrong term some time ago?

Thank you for your suggestions

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Andreas Huber

unread,
Aug 29, 2003, 4:34:42 AM8/29/03
to
Matthieu Paindavoine wrote:
> Hello,
>
> I am coding a < composite / visitor / combinator > sandwich, and I
> bumped again what seems a tough cookie: virtual member template
> functions are not allowed.
[snip]

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&frame=right&th=982ee5addad67d25&seekm=b16s9h%24hj8%241%40news-reader10.wanadoo.fr#link9
(virtual template method / design problem)

Regards,

Andreas

Torsten Robitzki

unread,
Aug 30, 2003, 7:25:29 PM8/30/03
to

It's not really clear to me what you want to achieve with this but the
obvious solution to me is to factor out the code that is common to all
types T.

class A
{
public:
template <class T>
void Op(T& _t)
{

std::cout<<common_code()<< _t.vis() << std::endl;
}
private:
virtual const char* common_code() const
{
return "A::Op ";
}
};

Last week on de.comp.lang.iso-c++ a similar problem arose and we found a
solution for a fixed number of types T by not making the virtual
function a template and to generate the class with a type list and a
class that describe the function body in a static function.

regards
Torsten

0 new messages