|
-
February 22nd, 2003, 12:35 AM
#1
Virtual Template Function
Hello,
I was wondering if it's possible to have a virtual template function. That is have a class, that has as one of its member functions a templated function that is also pure virtual.
ie.
Class A {
template <class T>
virtual int foo ( T & param ) = 0;
}
// class B is dervied from class A
Class B : public class A {
template <class T>
int foo (T & param) {
// implement foo...
}
}
When I arrange my code like this I get all sorts of errors, indicating that this is not allowed. Is there something i'm doing wrong? I dont see what the problem would be....any help at all would be appreciated!
thanks
Lil' Hash
-
February 22nd, 2003, 11:10 AM
#2
As far as I know, there cannot be templatized virtual member
functions.
--Paul
-
February 23rd, 2003, 06:40 AM
#3
well i tried to do it and i think this error explains it all
D:\experiments\exper\test.cpp(7) : error C2898: 'int __thiscall a::af(T)' : member function templates cannot be virtual
It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames
-
February 23rd, 2003, 07:11 AM
#4
Originally posted by SeventhStar
well i tried to do it and i think this error explains it all
D:\experiments\exper\test.cpp(7) : error C2898: 'int __thiscall a::af(T)' : member function templates cannot be virtual
In this example, you happen to be correct ... but don't rely upon
Microsoft's compiler when testing Standard-conformance in the
future; they've been known to be missing features in the past
-
February 23rd, 2003, 07:37 AM
#5
Think about it for a while: how could you make a templated virtual function? What's its signature? How many vtable entries do you reserve? How would you distinguish between an override/hide and an overload?
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 24th, 2003, 11:30 AM
#6
I believe it is not possible -- mainly for the reasons Graham pointed out.
BTW, even g++ won't compile the code -- it is not just a MS-specific problem!
- Kevin
-
February 24th, 2003, 01:23 PM
#7
Originally posted by KevinHall
I believe it is not possible -- mainly for the reasons Graham pointed out.
BTW, even g++ won't compile the code -- it is not just a MS-specific problem!
- Kevin
I hope I didn't imply that I thought it was.... I just meant that
something not working on a Microsoft compiler isn't usually good
enough reason to say "the standard doesn't allow this". Heck,
ANY one compiler isn't really good enough reason.
-
February 24th, 2003, 01:44 PM
#8
Paul,
No, I understood what you were saying. And I know that MS doesn't comply completely with the ANSI standard -- especially MSVC 6.0. I was just letting others know that g++ doesn't compile it either (perhaps I should have left that exclamation point off at the end. ). Personally, I think your responses are accurate and full of useful information. I hope there are no hard feelings!
- Kevin
-
February 24th, 2003, 03:48 PM
#9
Originally posted by KevinHall
Paul,
No, I understood what you were saying. And I know that MS doesn't comply completely with the ANSI standard -- especially MSVC 6.0. I was just letting others know that g++ doesn't compile it either (perhaps I should have left that exclamation point off at the end.  ). Personally, I think your responses are accurate and full of useful information. I hope there are no hard feelings!
- Kevin
Aw hey, not at all, my fellow poster. I didn't even mean to come
across as being defensive, but reading my post on my own ... it
definitely would appear that way.
Is it just me or does it seem like computer people don't know how
to communicate properly? Bah, it's probably just me 
--Paul
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|