title: Does reinterpret_cast together with binary input/output always leads to undefined behavior?
body: <p>When working binary input/output, we often see/write code similar to this</p>
  <pre><code>in_file.read(reinterpret_cast&lt;char *&gt;(&amp;obj), sizeof(obj))
  out_file.write(reinterpret_cast&lt;const char*&gt;(&amp;obj), sizeof(obj))
  </code></pre>
  <p>Now, is there any guarantee within the standard that these operations do not lead to undefined behavior?
  A clarification/explanation why it works would be appreciated.</p>
  <h3>Rational for the question:</h3>
  <p>According to <a href="https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_aliasing" rel="nofollow noreferrer">cppreference</a> (I hope I understood it correctly), casting T* to char*, unsigned char* and std::byte* and de-referencing the result should be legal. It explicitly states</p>
  <blockquote>
  <p>AliasedType is std::byte, (since C++17) char, or unsigned char: this
  permits examination of the object representation of any object as an
  array of bytes.</p>
  </blockquote>
  <p>Now what is not clear to me, is that sizeof(obj) part as well as <em>&quot;examination of the object representation as an array of bytes&quot;</em> imply that there is pointer arithmetic involved... which raises the question - is pointer arithmetic permitted by the standard? (In case of binary io I haven't looked at the actual implementations (they are a but unreadable to me at this point...), if there is alternative method used behind the scenes, feel free to clarify.</p>
  <p>In case it does lead to undefined behavior, should <code>bit_cast</code> to <code>std::array</code> be preferred when doing binary IO?</p>
  <p>I mean something like this:</p>
  <pre><code>auto binary_rep = std::bit_cast&lt;std::array&lt;const char, sizeof(obj)&gt;&gt;(obj);
  out_file.write(&amp;binary_rep[0], sizeof(obj));
  </code></pre>
  <p>for writing and</p>
  <pre><code>std::array&lt;char, sizeof(obj)&gt; binary_rep;
  in_file.read(&amp;binary_rep[0], sizeof(obj));
  obj = bit_cast(binary_rep);
  </code></pre>
  <p>for reading?</p>