473,804 Members | 3,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

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

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

class B : public A {
public:
template<typena me 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<typena me Tvirtual f() = 0;
}

class B : public A {
public:
template<typena me 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<De rivedT*>(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<De rivedT*>(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
12119
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 member function?
4
4651
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 class version from the base class pointer and still be able to use a vector with derived element pointers? class BaseElem { }; class DerivedElem1 { };
7
1816
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 have attached a small example. Is that bug or feature. If feature: is there an easy way to get the
7
1527
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
1749
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 Studio .NET. The problem is that for gcc the template functions (members of a class) are explicitly instantiated in a cpp file that includes the classes .h and .cpp file. The VS compiler does not correctly bring together the function template...
4
1507
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
1403
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 if it is not pure." The subsequent paragraph says this: "Every program shall contain exactly one definition of every non-inline function or object that is used in that program; no diagnostic required." Is that saying that a virtual member...
2
2061
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 error C1001: internal compiler error. // (File "msc1.cpp", Line 1392)
11
1894
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
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10310
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.