home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 33,091 of 33,346   
   Norman J. Goldstein to All   
   Re: template virtual methods   
   26 May 13 15:17:47   
   
   From: normvcr@telus.net   
      
   Many modern vector/matrix libraries use the CRTP to implement their class   
   hierarchies.  Suppose that you are using such a library to build a processing   
   system for Cars.  For example   
      
   class Car   
   {   
    // Does some kind of measurement, depending on the car.   
    template< class DERIVED>   
    void measure( Vector& vec );   
   };   
      
   class Fiat : public Car   
   {   
    // Here is the Fiat-specific implementation   
    template< class DERIVED>   
    void measure( Vector& vec );   
   };   
      
   The "measure" method cannot be virtual, since it is templated.  I don't want   
   to apply the CRTP approach to the Car hierarchy, since I want to be able to   
   have an array of mixed types of cars   
      
   Car* carArray;   
      
   I've looked into boost:any, and each instance stores the typeid of the value   
   being handled, its own virtual method, and a static cast finally to get access   
   to the desired value.   
      
   In the case of vector/matris libraries, there is a work-around involving   
   vector views, but, this is not a solution that generalizes.   
      
      
      
      
   On 05/26/2013 12:05 AM, Öö Tiib wrote:   
   > On Sunday, 26 May 2013 01:40:02 UTC+3, Norman J. Goldstein  wrote:   
   >> I did not see this covered in the FAQ, and I looked at many web   
   >> postings on this topic, I find that the common understanding is that   
   >> templates (compile-time) and the virtual mechanism (run-time) are   
   >> incompatible, and so template virtual methods are not supported.   
   >   
   > They are not incompatible. There are several techniques how to use   
   > both with same class. However both things are considered major source   
   > of complexities by many. Merging the features in a function is hyper   
   > complex (complexity in square).   
   >   
   >> However, if the construction of the virtual tables were delayed   
   >> until link time, the compiler/linker/loader would have the   
   >> information it needs to build the vtables.  Certainly, this has been   
   >> considered, so what is wrong with the idea?  Is it a matter of too   
   >> much complexity for too little gain?  I think it is worth it, but, I   
   >> am not writing the compilers :-)   
   >   
   > My bet goes to complexity and no gain. Can you provide example for   
   > what to use such a thing? I can't (so "no gain" part is just maybe my   
   > bad imagination).   
   >   
   > Lot of C++ programmers never write templates (they use, but do not   
   > write), base class with template virtual forces them to write   
   > one. They also frequently complain about complex compiler errors that   
   > are related to templates. I have no difficulty to imagine the pile of   
   > horrific linker errors about such virtual template in library   
   > somewhere.   
   >   
   >   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca