title: as-if rule and removal of allocation
body: <p>The "<a href="http://[1]%20[as_if%20rule][1]http://en.cppreference.com/w/cpp/language/as_if" rel="nofollow">as-if rule</a>" gives the compiler the right to optimize out or reorder expressions that would not make a difference to the output and correctness of a program under certain rules, such as; </p>
  
  <blockquote>
    <p>§1.9.5</p>
    
    <p>A conforming implementation executing a well-formed program shall
    produce the same observable behavior as one of the possible executions
    of the corresponding instance of the abstract machine with the same
    program and the same input.</p>
  </blockquote>
  
  <p>The cppreference url I linked above specifically mentions special rules for the values of volatile objects, as well as for "new expressions", under C++14:</p>
  
  <blockquote>
    <p>New-expression has another exception from the as-if rule: the compiler
    may remove calls to the replaceable allocation functions even if a
    user-defined replacement is provided and has observable side-effects.</p>
  </blockquote>
  
  <p>I assume "replaceable" here is what is talked about for example in </p>
  
  <blockquote>
    <p>§18.6.1.1.2</p>
    
    <p>Replaceable: a C++ program may define a function with this function
    signature that displaces the default version defined by the C++
    standard library.</p>
  </blockquote>
  
  <p>Is it correct that <code>mem</code> below can be removed or reordered under the as-if rule?  </p>
  
  <pre><code>  {
    ... some conformant code // upper block of code
  
    auto mem = std::make_unique&lt;std::array&lt;double, 5000000&gt;&gt;();
  
    ... more conformant code, not using mem // lower block of code
    }
  </code></pre>
  
  <p>Is there a way to ensure it's not removed, and stays between the upper and lower blocks of code? A well placed volatile (either/or volatile std::array or left of auto) comes to mind, but as there is no reading of <code>mem</code>, I think even that would not help under the <em>as-if</em> rule.</p>
  
  <p>Side note; I've not been able to get visual studio 2015 to optimize out <code>mem</code> and the allocation at all.</p>
  
  <p>Clarification: The way to <em>observe</em> this would be that the allocation call to the OS comes between any i/o from the two blocks. The point of this is for test cases and/or trying to get objects to be allocated at new locations.</p>