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,617 of 33,346   
   Seungbeom Kim to James Lothian   
   Re: The dangers of returning const refer   
   25 Oct 12 23:31:57   
   
   From: musiphil@bawi.org   
      
   On 2012-10-25 17:59, James Lothian wrote:   
   > It's   
   > a shame that std::map<> doesn't provide (as far as I can tell)   
   > a member fn that gets given a value and inserts a new entry if   
   > there isn't one, or overwrites the value of one that's there   
   > already. Operator[] is close to what you want, but the fact   
   > that the new map entry can be created before you've had a   
   > chance to create the value you're going to put in it seems to   
   > make it unexpectedly hard to use exception-safely.   
      
   If you finish creating the value to be inserted (in a separate   
   statement) before trying to access the map, it's very close to   
   what you want:   
      
        // given std::map m = ...; K k = ...;   
        V v = create_V(...);   
        m[k] = v;   
      
   I feel little need to write a separate function for this (except for   
   wanting to avoid the slightly inefficient combination of default   
   construction and assignment), because as you pass the value to the   
   function as an argument, you automatically finish creating it before   
   calling the function, and the function reduces to merely executing   
   'm[k] = v'.   
      
   And it's a good idea in general to separate potentially throwing   
   or modifying operations from one another (i.e. not mix them in a   
   single statement), anyway.   
      
   --   
   Seungbeom Kim   
      
      
         [ 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