Direct execution of the data
<p>What is the best approach to make my program to execute the data. Say, I wrote the (so-called) compiler for <strong>x86_64</strong> machine:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

#include &lt;cstdlib&gt;
#include &lt;cstdint&gt;

struct compiler
{

    void op() const { return; }

    template&lt; typename ...ARGS &gt;
    void op(std::uint8_t const _opcode, ARGS &amp;&amp; ..._tail)
    {
        code_.push_back(_opcode);
        return op(std::forward&lt; ARGS &gt;(_tail)...);
    }

    void clear() { code_.clear(); }

    long double operator () () const
    {
        // ?
    }

private :

    std::vector&lt; std::uint8_t &gt; code_;

};

int main()
{
    compiler compiler_; // long double (*)();
    compiler_.op(0xD9, 0xEE); // FLDZ
    compiler_.op(0xC3);       // ret
    std::cout &lt;&lt; compiler_() &lt;&lt; std::endl;
    return EXIT_SUCCESS;
}
</code></pre>

<p>But I don't know how to implement <code>operator ()</code> correctly. I suspect, that I must put all the contents of <code>code_</code> into contiguous memory chunk and then cast to <code>long double (*)();</code> and call this. But there is some difficulties:</p>

<ul>
<li>Should I use <code>VirtualProtect(Ex)</code> (+ <code>FlushInstructionCache</code>) on Windows? And something similar on Linux?</li>
<li>What is the container, that reliably places the bytes in the memory in proper manner (i.e. one by one)? And also allows to get the pointer to memory chunk.</li>
</ul>
