title: correct idiom for std::string constants?
body: <p>I have a map that represents a DB object. I want to get 'well known' values from it</p>
  
  <pre><code> std::map&lt;std::string, std::string&gt; dbo;
   ...
   std::string val = map["foo"];
  </code></pre>
  
  <p>all fine but it strikes me that "foo" is being converted to a temporary string on every call. Surely it would be better to have a constant std::string (of course its probably a tiny overhead compared to the disk IO that just fetched the object but its still a valid question I think). So what is the correct idiom for std::string constants?</p>
  
  <p>for example - I can have </p>
  
  <pre><code> const std::string FOO = "foo";
  </code></pre>
  
  <p>in a hdr, but then I get multiple copies</p>
  
  <p>EDIT: No answer yet has said how to declare std::string constants. Ignore the whole map, STL, etc issue. A lot of code is heavily std::string oriented (mine certainly is) and it is natural to want constants for them without paying over and over for the memory allocation</p>
  
  <p>EDIT2: took out secondary question answered by PDF from Manuel, added example of bad idiom</p>
  
  <p>EDIT3: Summary of answers. Note that I have not included those that suggested creating a new string class. I am disappointed becuase I hoped there was a simple thing that would work in header file only (like const char * const ). Anyway</p>
  
  <p>a) from Mark b</p>
  
  <pre><code> std::map&lt;int, std::string&gt; dict;
   const int FOO_IDX = 1;
   ....
   dict[FOO_IDX] = "foo";
   ....
   std:string &amp;val = dbo[dict[FOO_IDX]];
  </code></pre>
  
  <p>b) from vlad</p>
  
  <pre><code> // str.h
   extern const std::string FOO;
   // str.cpp
   const std::string FOO = "foo";
  </code></pre>
  
  <p>c) from Roger P</p>
  
  <pre><code> // really you cant do it
  </code></pre>
  
  <p>(b) seems the closest to what I wanted but has one fatal flaw. I cannot have static module level code that uses these strings since they might not have been constructed yet. I thought about (a) and in fact use a similar trick when serializing the object, send the index rather than the string, but it seemed a lot of plumbing for a general purpose solution. So sadly (c) wins, there is not <em>simple</em> const idiom for std:string</p>