title: C++ string that can be NULL
body: <p>I'm used to passing around string like this in my C++ applications:</p>
  
  <pre><code>void foo(const std::string&amp; input)
  {
    std::cout &lt;&lt; input.size() &lt;&lt; std::endl;
  }
  
  void bar()
  {
    foo("stackoverflow");
  }
  </code></pre>
  
  <p>Now I have a case where I want the string to be NULL:</p>
  
  <pre><code>void baz()
  {
    foo("stackoverflow");
    foo(NULL); // very bad with foo implementation above
  }
  </code></pre>
  
  <p>I could change <code>foo</code> to:</p>
  
  <pre><code>void foo(const std::string* input)
  {
    // TODO: support NULL input
    std::cout &lt;&lt; input-&gt;size() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>But to pass a string literal or copy a <code>char*</code> to that implementation of <code>foo</code> I need to write something like this:</p>
  
  <pre><code>void bar()
  {
    string input("hi"); // annoying temporary
    foo(&amp;input);
    foo(NULL);  // will work as long as foo handles NULL properly
  }
  </code></pre>
  
  <p>I started thinking about inheriting from <code>std::string</code> and adding a <code>null</code> property, but I'm not so sure it's a good idea. Maybe it is better to simply use a <code>const char*</code> string for parameters that can be NULL, but what if I want to save a copy of the string (or NULL) without having to manage its memory myself? (See <a href="https://stackoverflow.com/questions/312570/what-are-some-of-the-drawbacks-to-using-c-style-strings">What are some of the drawbacks to using C-style strings?</a> etc.)</p>
  
  <p>Any clever solution around?</p>