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,125 of 33,346   
   Ian Collins to K. Frank   
   Re: What are some simple, practical exam   
   14 Jul 13 23:36:41   
   
   From: ian-news@this.is.invalid   
      
   K. Frank wrote:   
   >   
   > Hello Group!   
   >   
   > What are some simple examples that illustrate the benefits   
   > of template metaprogramming?   
   >   
   > What I have in mind would be examples that "speak to"   
   > programmers (like me) who don't have much experience   
   > with template metaprogramming.   
   >   
   > I'm looking for relatively simple programming challenges   
   > that most programmers would have encountered for which   
   > template metaprogramming would offer a good -- or even   
   > compelling -- solution.   
   >   
   > So the classic textbook example of calculating factorials   
   > at compile time, while cool, isn't really a good example   
   > in this sense because it's contrived.   
      
   It may be contrived, but the principal is sound.  If you want to loop at   
   compile time, TMP solutions are the only way to go.   
      
   Here's an equally simple example from the function mocking framework I   
   use for unit tests.  In the framework, all the test objects derive from   
   a simple class template:   
      
       template    
       struct TestFunction   
       {   
         // Common data.   
      
         static void setup() { // initialise common data }   
       };   
      
   where N is a sequential value.  At the start of each test I want to   
   initialise all of the mock objects, so given N is sequential, I can   
   simply write:   
      
       template    
       void setUpMock();  // Specialised elsewhere.   
      
       template    
       inline void doSetUp()   
       {   
         setUpMock();   
         doSetUp();   
       }   
      
       template <>   
       inline void doSetUp<0>()   
       {   
         setUpMock<0>();   
       }   
      
       inline void setUp() { doSetUp(); }   
      
   and call setUp() to initialise the lot.   
      
   A simple text book real world example.   
      
   --   
   Ian Collins   
      
      
         [ 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