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

virtual member function templates

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 Tvirtual f() = 0;
}

class B : public A {
public:
template<typename Tf();
}

class C : public A {
public:
template<typename Tf();
}

That is, I would like to force subclasses to implement a templated
member function f.
Sep 14 '06 #1
5 2936
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 Tvirtual f() = 0;
}

class B : public A {
public:
template<typename Tf();
}

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

Sep 14 '06 #2
dj
Mi*************@tomtom.com 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 Tvirtual f() = 0;
}

class B : public A {
public:
template<typename Tf();
}

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?
Sep 14 '06 #3

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

Sep 14 '06 #4
dj
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.
Sep 14 '06 #5

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

Sep 15 '06 #6

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

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
7
by: Karsten Hochkirch | last post by:
Hi, I have just encountered a problem when calling a virtual member function in a constructor. Only the memberfunction of the parent class is called instead of the overloaded function. I...
7
by: christopher diggins | last post by:
Hi everyone, I have the following code (greatly simplified to demonstrate issue) : #include <iostream> class CBaseFuBar { public: virtual void Fu() { std::cout << "base"; }; };
1
by: Arne Petersen | last post by:
Hy, I've got a problem with member function templates compiled into libraries. I'm trying to get a library collection (coded for GNU gcc, where its compiled completly) being compiled on Visual...
4
by: Rajan | last post by:
Hi All C++ Experts Can anybody share some of your thoughts in Member Function Templates implementation.Can you also give some example on it Best Regards Raj
2
by: Steven T. Hatton | last post by:
This is under the heading of "One Definition Rule" in the Standard, in a paragraph explaining what it means for an object or non-overloaded function to be 'used': "A virtual member function is used...
2
by: pascal.zschumme | last post by:
hello folks My problem is that the following code using something very very difficult technique :D fails to compile on MSVC8: <code> // main.cpp // // the error is: // main.cpp(8) : fatal...
11
by: Gabriel de Dietrich | last post by:
Hi all! Just out of curiosity: Is there any way to get the address of a particular virtual member function given an object? Some code to make things more clear... class A { public: A() { }
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.