title: How do I mix constants and adhoc return values efficiently?
body: <p>I'm trying to get back into C++ but it's changed a bit in 20 years</p>
  
  <p>I am processing a network stream that has a bunch of strings.</p>
  
  <p>I know what the vast majority of these strings could be, but not all of them</p>
  
  <p>If I knew what all the strings could be, I would map these strings to an enum so that comparisons are cheap and I would not have millions of repeated strings in memory</p>
  
  <p>As I know what most of the strings could be, I thought i would create a file with a bunch of const std::strings and dynamically create the strings I don't know about. However, I seem to be copying the const std::string rather than linking directly to them</p>
  
  <p>I wrote a silly program to demonstrate.</p>
  
  <p>I used cities below. I know about Seattle and Tacoma, but not about the random cities.</p>
  
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  #include &lt;random&gt;
  
  const std::string SEATTLE("Seattle");
  
  const std::string TACOMA("Tacoma");
  
  std::random_device rd;
  
  std::mt19937 gen(rd());
  
  std::uniform_int_distribution&lt;int&gt; dist(0, 'Z' - 'A');
  
  auto next = []() -&gt; char {
  
      return static_cast&lt;char&gt;('A' + dist(gen));
  };
  
  const std::string factory(int in) {
  
      switch (in) {
          case 0: {
              return SEATTLE;
          }
          case 1: {
              return TACOMA;
          }
          default: {
  
              std::string retval(5, 0);
  
              std::generate_n(retval.begin(), 5, next);
  
              return retval;
          }
      }
  }
  
  int main(int argc, char **argv) {
  
      std::cout &lt;&lt; "SEATTLE=" &lt;&lt; &amp;SEATTLE &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "TACOMA=" &lt;&lt; &amp;TACOMA &lt;&lt; std::endl;
  
      for (int i = 0; i &lt; 4; ++i) {
  
          for (int j = 0; j &lt; 4; ++j) {
  
              const std::string value = factory(i);
  
              std::cout &lt;&lt; "i=" &lt;&lt; i &lt;&lt; " " &lt;&lt; value &lt;&lt; " " &lt;&lt; &amp;value &lt;&lt; std::endl;
          }
      }
  }
  </code></pre>
  
  <p>What I want is my global Seattle and Tacoma to be returned, not a copy</p>
  
  <p>How do I avoid copying constant strings yet maintain the same interface for strings I don't know about?</p>
  
  <p>Or should I not care?</p>
  
  <p>Thanks</p>