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 31,995 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Specializing a template for a base a   
   07 Mar 12 16:47:51   
   
   From: daniel.kruegler@googlemail.com   
      
   Am 07.03.2012 21:21, schrieb Seungbeom Kim:   
   > On 2012-03-04 15:10, Daniel Krügler wrote:   
   >>   
   >> Ensure that your primary template is a slightly worse match (even in the   
   >> absence of constraints), then add constrained overloads. Here is an   
   >> example of what I'm thinking of:   
   >>   
   >> #include   
   >> #include   
   >>   
   >> struct Animal {};   
   >> struct Dog : Animal {};   
   >> struct Fruit {};   
   >> struct Apple : Fruit {};   
   >> struct Other {};   
   >>   
   >> template   
   >> typename std::enable_if::value>::type   
   >> do_action(T ptr) { std::cout<<  "generic"<<  std::endl; }   
   >>   
   >> template   
   >> inline void action(T* ptr) { do_action(ptr); }   
   >>   
   >> template   
   >> typename std::enable_if::value>::type   
   >> do_action(T* ptr) { std::cout<<  "animals"<<  std::endl; }   
   >>   
   >> template   
   >> typename std::enable_if::value>::type   
   >> do_action(T* ptr) { std::cout<<  "fruits"<<  std::endl; }   
   >   
   > Thank you very much, that works indeed.   
   >   
   > One question I have is: is there any reason you introduced an extra level   
   > of indirection, namely do_action? That is, wouldn't this ...   
   >   
   > template   
   > typename std::enable_if::value>::type   
   > action(T ptr) { std::cout<<  "generic"<<  std::endl; }   
   >   
   > template   
   > typename std::enable_if::value>::type   
   > action(T* ptr) { std::cout<<  "animals"<<  std::endl; }   
   >   
   > template   
   > typename std::enable_if::value>::type   
   > action(T* ptr) { std::cout<<  "fruits"<<  std::endl; }   
   >   
   > ... work as well? I tried it, and it seemed to work.   
      
   Sure. The reason why I suggested the indirection was just to provide a   
   simpler view on the primary function (For someone looking at the code it   
   may be astonishing to see this construction). It would suffice to add a   
   corresponding documentation to explain this, though.   
      
   Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
   --   
         [ 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