title: Nullable values in C++
body: <p>I'm creating a database access layer in native C++, and I'm looking at ways to support NULL values.  Here is what I have so far:</p>
  
  <pre><code>class CNullValue
  {
  public:
      static CNullValue Null()
      {
          static CNullValue nv;
  
          return nv;
      }
  };
  
  template&lt;class T&gt;
  class CNullableT
  {
  public:
      CNullableT(CNullValue &amp;v) : m_Value(T()), m_IsNull(true)
      {
      }
  
      CNullableT(T value) : m_Value(value), m_IsNull(false)
      {
      }
  
      bool IsNull()
      {
          return m_IsNull;
      }
  
      T GetValue()
      {
          return m_Value;
      }
  
  private:
      T m_Value;
      bool m_IsNull;
  };
  </code></pre>
  
  <p>This is how I'll have to define functions:</p>
  
  <pre><code>void StoredProc(int i, CNullableT&lt;int&gt; j)
  {
      ...connect to database
      ...if j.IsNull pass null to database etc
  }
  </code></pre>
  
  <p>And I call it like this:</p>
  
  <pre><code>sp.StoredProc(1, 2);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>sp.StoredProc(3, CNullValue::Null());
  </code></pre>
  
  <p>I was just wondering if there was a better way than this.  In particular I don't like the singleton-like object of CNullValue with the statics.
  I'd prefer to just do </p>
  
  <pre><code>sp.StoredProc(3, CNullValue);
  </code></pre>
  
  <p>or something similar.  How do others solve this problem?</p>