Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is basically a question of how to handle <a href="http://en.wikipedia.org/wiki/Partial_function" rel="nofollow">non-total functions</a>—functions that are not necessarily defined for all possible inputs. There are many ways to go about it, though none of them is type-safe:</p> <ol> <li><p>Have two separate functions for retrieving and erasing the top element of the stack, much like <a href="http://en.cppreference.com/w/cpp/container/stack/top" rel="nofollow"><code>std::stack::top()</code></a> and <a href="http://en.cppreference.com/w/cpp/container/stack/pop" rel="nofollow"><code>std::stack::pop()</code></a>.</p></li> <li><p>Use a reference parameter and return a <code>bool</code> indicating whether an element was returned:</p> <pre><code>bool pop(T&amp; value) { if (stack.empty()) return false; value = stack.back(); stack.pop_back(); return true; } </code></pre> <p>The caller can write:</p> <pre><code>T value; if (stack.pop(value)) { use(value); } </code></pre></li> <li><p>Return a <a href="http://www.boost.org/doc/libs/1_53_0/libs/optional/doc/html/boost_optional/examples.html#boost_optional.examples.optional_return_values" rel="nofollow"><code>boost::optional&lt;T&gt;</code></a>.</p></li> <li><p>Return a default-constructed <code>T</code> instance. This requires that <code>T</code> be default-constructible, which is already imposed by some <a href="http://en.cppreference.com/w/cpp/container/vector" rel="nofollow"><code>std::vector</code></a> member functions.</p></li> <li><p>Throw an exception, documenting that a precondition was violated.</p></li> <li><p>Leave the behaviour of popping an empty stack undefined.</p></li> </ol> <p>I recommend #1, #2, or #3, depending on preference and what fits best with the surrounding code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload