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,839 of 33,346   
   Ulrich Eckhardt to All   
   Re: Template argument deduction   
   24 Jan 12 00:35:48   
   
   26f0200d   
   From: ulrich.eckhardt@dominolaser.com   
      
   Am 24.01.2012 02:41, schrieb kelvSYC:   
   > I'm having a problem where it would seem like template arguments could   
   > be deduced, but in reality the compiler barfs because it can't.   
   > Suppose I have this generic functor:   
   >   
   > struct HeapConvertInserter {   
   >     template   
   >     shared_ptr  operator()(const Key&  key) { ... }   
   > };   
      
   For the record, "Value" will not be deduced here, because it is not used   
   in the argument list of the function. There are several things that the   
   compiler could use:   
   1. The implementation of operator()   
   This won't be used because in the place where it is used, that   
   implementation might be unavailable. Also, there is nothing that keeps   
   two different instantiations from having the same implementation.   
   2. The assignee of the result   
   Since you can always ignore returned values, this isn't guaranteed to be   
   present. Also, if the assignee has a template assignment operator, you   
   would be lost again.   
      
      
   > Now, consider a class which wraps a std::map>   
   > and some kind of delegate class like HeapConvertInserter.  One method   
   > which uses the two goes as follows:   
   >   
   > /* table is the std::map>  instance, delegate is   
   > the HeapConvertInserter instance */   
   > if (table.count(key) == 0) {   
   >     table.insert(std::map>::value_type(key,   
   > delegate(key)));   
   > }   
   >   
   > When I try to compile, apparently it can't deduce the Value template   
   > argument of the HeapConvertInserter's operator(), even though it would   
   > appear that Value should appear as, well, Value as defined in the   
   > std::map instance.  Why is this happening?   
      
   No, sorry. You can reuse the name "Value" in many different places and   
   using it in one template won't automatically affect another.   
      
   > Is there a way to make this work without moving the Value off of the   
   > operator() template and into the class template?   
      
   Yes, see recent thread you started yourself, i.e. using explicit   
   template arguments. Note that if you don't want to repeat yourself, you   
   could reference the type inside the map using   
   key_type/value_type/element_type typedefs.   
      
   Uli   
      
      
   --   
         [ 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