title: C++ Behavior of priority_queue pop() and top() when queue is empty
body: <p>C++11. </p>
  
  <p>If I try to <code>pop</code> off of an empty <code>priority_queue,</code> then I get a compile time error. Specifically,  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;queue&gt;
  
  int main()
  {
      std::priority_queue&lt;int&gt; pq;
      int x = pq.pop();
      std::cout &lt;&lt; x &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>yields the compile-time error: </p>
  
  <pre><code>In function 'int main()': 7:20: error: void value not ignored as it ought to be
  </code></pre>
  
  <p>I am surprised that this is a compile-time error and not a runtime error, but that is another discussion.   </p>
  
  <p>What I don't understand is why the <code>top</code> method wouldn't do the same thing. In fact, if we change <code>pop</code> to <code>top</code> the code actually compiles and runs (albeit with a bizzare output). Specifically, </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;queue&gt;
  
  int main()
  {
      std::priority_queue&lt;int&gt; pq;
      int x = pq.top();
      std::cout &lt;&lt; x &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>This code compiles, runs, and exits normally, but produces no output! It literally prints nothing. I do not understand how that can be. </p>
  
  <p>Here are my questions: </p>
  
  <ol>
  <li><p>Is the behavior for <code>pop</code> and <code>top</code> defined if the <code>priority_queue</code> is empty? I don't see any mention of this condition <a href="https://en.cppreference.com/w/cpp/container/priority_queue/top" rel="nofollow noreferrer">in the docs</a>.</p></li>
  <li><p>How is there no output in the second case? <code>top</code> returns a constant reference. So it must be pointing at something. I would imagine that it simply interprets whatever piece of memory it is pointing to as an integer. But that doesn't seem to be happening. It seems like the integer is behaving something like a <code>nullptr</code>. </p></li>
  </ol>