Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual member function templates

Reply
Thread Tools

virtual member function templates

 
 
dj
Guest
Posts: n/a
 
      09-14-2006
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.
 
Reply With Quote
 
 
 
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      09-14-2006
dj wrote:
> 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

 
Reply With Quote
 
 
 
 
dj
Guest
Posts: n/a
 
      09-14-2006
wrote:
> dj wrote:
>> 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
>


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?
 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      09-14-2006

dj wrote:

> 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

 
Reply With Quote
 
dj
Guest
Posts: n/a
 
      09-14-2006
werasm wrote:
> dj wrote:
>
>> 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
>


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.
 
Reply With Quote
 
werasm
Guest
Posts: n/a
 
      09-15-2006

dj wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing virtual member function to templates g3rc4n@gmail.com C++ 2 03-20-2009 04:23 PM
same overhead in calling virtual and non virtual member function...? ypjofficial@indiatimes.com C++ 11 09-10-2006 06:08 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
container of pointers and virtual member function templates Sat C++ 4 11-13-2003 11:44 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57