title: Simple C++ cross-platform way to execute an external program
body: <p>What is a simple, elegant, and effective cross-platform way to execute an external program in C++ and get the return code from it?</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int execute(std::string const &amp;path, std::vector&lt;std::string&gt; const &amp;arguments = {})
  {
      //...
  }
  </code></pre>
  
  <p>Since we're waiting for the called program to finish before continuing execution, the called program should use our program's input/output/error streams. If, for any number of reasons, <code>path</code> isn't executable, just throw an exception (e.g. <code>std::invalid_argument</code>).</p>
  
  <p>Obviously, don't use <code>system()</code>.</p>