title: Why doesn't QList::at() check if index exists and also returns a read-only value?
body: <p>This question is more of an inquiry then actually seeking a solution for a problem.</p>
  
  <p><code>QList::at()</code> not only doesn't check if an index is out of bounds but it also returns a <code>const</code> hence it can be used only in a <code>read-only</code> scenario:</p>
  
  <blockquote>
    <p><strong>const T &amp;QList::at(int i) const</strong></p>
    
    <p>Returns the item at index position i in the list. <strong>i must be a valid
    index position in the list</strong> (i.e., 0 &lt;= i &lt; size()).</p>
    
    <p>This function is very fast (constant time).</p>
  </blockquote>
  
  <p>I've just found out these peculiar implementation details of <code>QList</code> while trying to assign a new value to an element in my list and I'm wondering if there is a reason for doing that.</p>
  
  <h2><strong>No check for index validity</strong></h2>
  
  <p>In <code>C++ STL</code> if we take <code>std::array</code> or <code>std::vector</code> (I'm not taking <code>std::list</code> since it doesn't have <code>std::list::at()</code> at all) we have the following:</p>
  
  <blockquote>
    <p><strong>std::vector::at</strong></p>
    
    <p>The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not</p>
  </blockquote>
  
  <p>At first I thought that the check is not included to ensure the "This function is very fast (constant time)." however after looking at the implementation of <code>QList</code> I have to say even if the check was included a constant time (and high speed) is most certainly ensured.</p>
  
  <p>A check for out of bounds would require two things (as far as I know):</p>
  
  <ul>
  <li>Check if given index is &lt; 0 - this is a constant time check (<code>O(1)</code> if we use some wacky notation). Obviously we can't have negative index (we can in <code>Python</code> but it has another meaning ;))</li>
  <li><p>Check if given index is &lt; <code>QList::size()</code> - constant time is also present here since the way <code>QList::size()</code> is implemented is:</p>
  
  <pre><code>inline int size() const Q_DECL_NOTHROW { return d-&gt;end - d-&gt;begin; }
  </code></pre>
  
  <p>This is again <code>O(1)</code> (if I'm not mistaken) since both values are stored inside the <code>QList::d</code> parameter which is a pointer to</p>
  
  <pre><code>struct Data {
    QtPrivate::RefCount ref;
    int alloc, begin, end;
    void *array[1];
  };
  </code></pre>
  
  <p>so accessing those and calculating their difference is not hurting the performance that much (obviously it has some minor impact since it introduces a couple of access and arithmetic operations plus backing up and restoring the stack due to the jump inside the <code>QList::size()</code> function).</p></li>
  </ul>
  
  <p>So why dump the check for index validity? Is the performance impact actually that big?</p>
  
  <h2><strong>Returning a read-only value</strong></h2>
  
  <p>By doing so the <code>QList::at()</code> function offers a hidden and (omho) not very useful feature which makes the difference in using it and <code>QList::[]</code> even more confusing. Also the <code>C++ STL</code> equivalent for <code>std::vector</code> and <code>std::array</code> allows assignment of new values using this function.</p>