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,904 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Template parameter visibility in spe   
   11 Feb 12 16:33:57   
   
   03c4a44d   
   From: daniel.kruegler@googlemail.com   
      
   Am 11.02.2012 10:43, schrieb Vidar Hasfjord:   
   > It would be useful to have the primary template parameters visible in   
   > a specialization. Now you need to restate the provided arguments if   
   > you need them within the specialization, and this is a maintenance   
   > hazard. For example:   
   >   
   >   template   
   >   struct TDispatch;   
   >   
   >   template<>  struct TDispatch<42>   
   >   {   
   >     enum {Id = MessageId}; // Error: MessageId is undefined.   
   >     //enum {Id = 42}; // Works, of course, but error-prone.   
   >   };   
   >   
   >   int main()   
   >   {return TDispatch<42>::Id;}   
   >   
   > Is there a good reason why the parameters are not visible?   
      
   I would find it *very* astonishing and problematic, if renaming   
   the parameter name of a primary template would have impact on a   
   specialization where this parameter would be somehow implied. The   
   specialization TDispatch<42> is a class type, no class template anymore,   
   so why bother about names in a completely unrelated type family?   
      
   Further, this mapping would very much depend on the kind of declaration   
   that the specialization sees, e.g. consider   
      
   // Header 1:   
      
   template   
   struct TDispatch;   
      
   // Header 2:   
      
   template   
   struct TDispatch;   
      
   // Header 3:   
      
   template   
   struct TDispatch;   
      
   All three declarations of the TDispatch template are equivalent. Noe   
   consider   
      
   // Header for specialization   
      
   #include  // I=1, 2, or 3   
      
   const int MessageId = 42;   
      
   template<>  struct TDispatch<42>   
   {   
         enum {Id = MessageId};   
   };   
      
   The meaning of this program would now strongly depend on the form of the   
   declaration of the primary template - this looks extremely dangerous to me.   
      
   As the last example demonstares, there are much easier way to ensure   
   consistency, e.g.   
      
   const int MyMessageId = 42;   
      
   template<>  struct TDispatch   
   {   
         enum {Id = MyMessageId};   
   };   
      
   HTH & 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