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 32,938 of 33,346   
   DeMarcus to All   
   Re: Can I overload with unused arguments   
   19 Mar 13 12:41:29   
   
   From: use_my_alias_here_at_hotmail_com@tellus.orb.dotsrc.org   
      
   >>   
   >>    struct Washer {   
   >>       // washers wash ;-)   
   >>       void wash();   
   >>        // sometimes with soap   
   >>       void wash( Soap& );   
   >>    };   
   >>   
   >>    struct SoapEater {   
   >>       // will throw you with heavy things if you give no real soap   
   >>       void eat( std::unique_ptr );   
   >>    };   
   >>   
   >> Usage:   
   >>   
   >>       Washer w;   
   >>       SoapEater e;   
   >>       std::unique_ptr soap = searchForSoap(); // unsure if found   
   >>   
   >>       if (!soap)   
   >>       {   
   >>           w.wash();   // washer can wash with water only   
   >>           // leave eater alone   
   >>       }   
   >>       else   
   >>       {   
   >>           w.wash( *soap );   
   >>           e.eat( std::move(soap) );   
   >>       }   
   >   
   > It was not clear in the OP's description whether the presence of   
   > an entertainer was determined at run-time, but if the presence of   
   > soap is determined at run-time as in your example above, I would   
   > simply go for a pointer argument.   
   >   
   >       void Washer::wash(Soap* soap)   
   >       {   
   >           if (soap == NULL) {   
   >               // wash without soap   
   >           }   
   >           else {   
   >               // wash with *soap   
   >           }   
   >       }   
   >   
      
   The Entertainer in my case is determined in compile-time. To go straight   
   to my problem, I have a class that takes a handle and a function with   
   how to deallocate the handle, like this.   
      
   class HandleHolder   
   {   
   public:   
      HandleHolder( int handle, std::function deallocator );   
   };   
      
   I /can/ use it like this.   
      
   HandleHolder myHandleHolder( someHandle, nullptr );   
      
   but I want to find a way to avoid using nullptr here since that forces a   
   documentation lookup what argument two actually is. If I instead write   
   something like:   
      
   HandleHolder myHandleHolder( someHandle, NO_DEALLOCATION );   
      
   then it's much more clear what's going on and I don't need to check the   
   documentation. If overloading the constructor so I can use:   
      
   HandleHolder myHandleHolder( someHandle );   
      
   is also not a good idea since it won't catch my attention that no   
   deallocation is used.   
      
   Tiib's robust_ptr could probably solve this, but let's say I want to   
   make HandleHolder generic like below, then I don't know how to use   
   robust_ptr in a simple way.   
      
   template   
   class HandleHolder   
   {   
   public:   
      HandleHolder( T handle, std::function deallocator );   
   };   
      
      
   /Daniel   
      
      
   --   
         [ 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