title: C++ type aliasing, where value is replaced
body: <p>Is the following code legal in C++?</p>
  
  <pre><code>int get_i(int idx) { ... }
  float transform(int i) { ... }
  void use(float f) { ... }
  
  static_assert(sizeof(int) == sizeof(float));
  void* buffer = std::malloc(n * sizeof(int));
  
  int* i_buffer = reinterpret_cast&lt;int*&gt;(buffer);
  float* f_buffer = reinterpret_cast&lt;float*&gt;(buffer);
  
  // Fill int values into the buffer
  for(int idx = 0; idx &lt; n; ++idx)
      i_buffer[idx] = get_i(idx);
  
  // Transform int value to float value, and overwrite
  // (maybe violates strict aliassing rule?)
  for(int idx = 0; idx &lt; n; ++idx)
      f_buffer[idx] = transform(i_buffer[idx]);
  
  for(int idx = 0; idx &lt; n; ++idx)
      use(f_buffer[idx]);
  </code></pre>
  
  <p>The second step reads the buffer value as an <code>int</code>, and then writes a <code>float</code> in its place. It never accesses the memory through <code>i_buffer</code> again afterwards, so there is no type aliasing when reading.</p>
  
  <p>However the assignment <code>f_buffer[idx] =</code> writes a <code>float</code> object into an <code>int</code> object, which is UB.</p>
  
  <p>Is there a way to make the compiler consider this to mean that the lifetime of the <code>int</code> should end, and a <code>float</code> object should be constructed in its place, so that there is no type aliassing?</p>