Get and set internal data: return type, behaviour in case of error
<p>Given is a C++ class holding internal data that shall be exposed to the outside and that shall be also modifiable.</p>

<pre><code>class Container {
  public:
    ... get( int key ) const;
    ... set( int key, int value );
  private:
    std::vector&lt;int&gt; m_values;
};
</code></pre>

<p>The question is what return type <code>get()</code> and <code>set()</code> preferably get. For <code>get()</code>:</p>

<ol>
<li><code>const int&amp; get( key )</code>, reference to const value.</li>
<li><code>const int* get( key )</code>, pointer to const value.</li>
</ol>

<p>When using the reference, something must happen when the given key does not exist in the container. Exception? Or just access <code>m_values</code> and let it throw? Rely on <code>assert</code>s?</p>

<p>The pointer return type can return <code>nullptr</code> indicating an invalid operation. However wouldn't this mean the caller did something wrong in general by requesting something that isn't there?</p>

<p>For <code>set()</code>:</p>

<ol>
<li><code>void set( key, value )</code></li>
<li><code>bool set( key, value )</code></li>
</ol>

<p>The <code>void</code> version ignores invalid keys or values. Again: What to do in case of an error?</p>

<p>The <code>bool</code> version returns <code>true</code> on success and <code>false</code> when key and/or value are invalid. Also the same as for <code>get()</code> here: The user probably did something wrong when specifying an invalid key and/or value.</p>

<p>Would you rather let the application crash somehow to indicate the user used it wrong or rely on the user checking result return values?</p>
