virtual member function templates

D

dj

I somehow understand why member function templates cannot be virtual,
but my question is how I could achieve something similar to this:

class A {
public:
template<typename T> virtual f() = 0;
}

class B : public A {
public:
template<typename T> f();
}

class C : public A {
public:
template<typename T> f();
}

That is, I would like to force subclasses to implement a templated
member function f.
 
M

Michiel.Salters

dj said:
I somehow understand why member function templates cannot be virtual,
but my question is how I could achieve something similar to this:

class A {
public:
template<typename T> virtual f() = 0;
}

class B : public A {
public:
template<typename T> f();
}

That is, I would like to force subclasses to implement a templated
member function f.

Forcing subclasses isn't the problem. How are you going to convince
the compiler to instantiate B::f<int>? The compiler only sees a call to
A::f<int>.

HTH,
Michiel Salters
 
D

dj

Forcing subclasses isn't the problem. How are you going to convince
the compiler to instantiate B::f<int>? The compiler only sees a call to
A::f<int>.

HTH,
Michiel Salters

I think I understand what you are saying. If I use:

B b;
A* pb = &b;
pb->f<int>();

then the compiler would instantiate A::f<int>, but not B::f<int>. If f
is pure this would be illegal anyway, so that is probably why templated
virtuals are not allowed in the first place.

However, let me repeat my question - how could I force the subclasses to
implement some function f (which is what i use the pure virtual for),
which itself should be templated? I could declare all possible function
prototypes instead of using a template, but is there a more elegant
solution?
 
W

werasm

dj said:
However, let me repeat my question - how could I force the subclasses to
implement some function f (which is what i use the pure virtual for),
which itself should be templated? I could declare all possible function
prototypes instead of using a template, but is there a more elegant
solution?

struct Base
{
template <class DerivedT, class T>
void foo()
{
DerivedT* derived( dynamic_cast<DerivedT*>(this) );
derived->template f<T>();
}
virtual ~Base(){}
};

struct Derived : Base
{
template <class T>
void f(){ ; }
};

void test_a()
{
Derived d;
Base& b( d );
b.foo<Derived, int>();
}

Derived is forced to implement f which is itself templated.... I don't
know whether this is usefull, but that seems to do what you ask.
Interesting syntax... Compiles on Comeau.

Regards,

W
 
D

dj

werasm said:
struct Base
{
template <class DerivedT, class T>
void foo()
{
DerivedT* derived( dynamic_cast<DerivedT*>(this) );
derived->template f<T>();
}
virtual ~Base(){}
};

struct Derived : Base
{
template <class T>
void f(){ ; }
};

void test_a()
{
Derived d;
Base& b( d );
b.foo<Derived, int>();
}

Derived is forced to implement f which is itself templated.... I don't
know whether this is usefull, but that seems to do what you ask.
Interesting syntax... Compiles on Comeau.

Regards,

W

This code really does the thing, thank you. I agree it is very weird,
though. Especially the call "derived->template f<T>();" is something new
to me. I tried and it works as "derived->f<T>();", too.

What I need such uncommon construction for is to have many different
algorithms implemented in different subclasses of some base class. The
templated function f would return the formatted result of any algorithm
in different possible data types. I am sure there are many other ways to
achieve that but this one seemed straightforward to me. Obviously I was
mistaken.
 
W

werasm

dj said:
This code really does the thing, thank you. I agree it is very weird,
though. Especially the call "derived->template f<T>();" is something new
to me. I tried and it works as "derived->f<T>();", too.

The other way is technically correct. If the compiler compiles
derived->f<T>(), which VC++7.1 does do, its not iaw. standard. The
template syntax is for the compiler to discern that < is not a
relational operator, I think. Someone else can perhaps comment (my time
is limited right now).

Regards,

Werner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top