From: bouncer@dev.null   
      
   Seungbeom Kim wrote:   
      
   > On 2012-07-15 16:05, Wil Evers wrote:   
   >>   
   >> I think the key is to make sure the A instance is closed for   
   >> modification before it is shared. The following class template is   
   >> my take at expressing that:   
   >>   
   >> template    
   >> class shared_immutable_ptr {   
   >>   
   >> public :   
   >> template    
   >> shared_immutable_ptr(std::unique_ptr consumed)   
   >> : instance(std::move(consumed))   
   >> { }   
   >   
   > Since you cannot copy std::unique_ptr, you need a reference   
   > parameter.   
      
   Well, I'll admit I'm still getting used to move semantics. However,   
   my understanding is that, because std::unique_ptr has a move   
   constructor while its copy constructor is deleted, the constructor   
   argument above will bind to rvalues of std::unique_ptr,   
   while not binding to lvalues. g++-4.7.1 appears to agree with me.   
      
   The intent is that shared_immutable_ptr acts as a sink, taking   
   ownership away from the unique_ptr passed to its constructor. Perhaps   
   I should have written   
      
    template    
    shared_immutable_ptr(std::unique_ptr&& consumed)   
    : instance(std::move(consumed))   
    { }   
      
   In practice, I think both forms are mostly equivalent here.   
      
   >> const T *operator->() const   
   >> { return instance.operator->(); }   
   >>   
   >> const T& operator*() const   
   >> { return *instance; }   
   >>   
   >> // etc...   
   >>   
   >> private :   
   >> std::shared_ptr instance;   
   >> };   
   >   
   > How is shared_immutable_ptr different from   
   > std::shared_ptr?   
      
   The difference is that there is a conversion from shared_ptr to   
   shared_ptr, while there is no conversion from shared_ptr   
   to shared_immutable_ptr. The effect is that the T instance passed   
   to it is marked as const before it becomes shared.   
      
   In other words, when compared to shared_ptr,   
   shared_immutable_ptr provides the additional guarantee that there   
   are no other smart pointers around that could act as a backdoor for   
   modifying the object pointed to.   
      
   Regards,   
      
   - Wil   
      
      
   --   
    [ 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)   
|