CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2002
    Posts
    23

    Unhappy 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

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    As far as I know, there cannot be templatized virtual member
    functions.

    --Paul

  3. #3
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    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

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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


  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    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

  7. #7
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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.

  8. #8
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    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

  9. #9
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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
  •  





Click Here to Expand Forum to Full Width

Featured