<div dir="ltr">Hi, I mean a specific valid runtime code should be able to run at compile time. Nothing more. The following code is pretty much valid, but you can&#39;t do it in compile time:<br>```cpp<br>alignas(alignof(T)) std::byte data[sizeof(T)];<br>new(&amp;data) T();<br>std::launder(reinterpret_cast&lt;T*&gt;(&amp;data))-&gt;~T();<br>```<br><br>Compilers can detect overflow, memory leak, etc and don&#39;t allow it in compile time. I&#39;d like to see a similar approach. Allow both placement new and reinterpret_cast, if usage is valid. But that&#39;s too big of a ask I think. So I thought something like std::typeless_memory or std::typeless_storage could be something easier to implement and manage</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Sat, Nov 30, 2024 at 10:25 PM Sebastian Wittmeier via Std-Proposals &lt;<a href="mailto:std-proposals@lists.isocpp.org">std-proposals@lists.isocpp.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg3832303662568888764"><u></u>

  
  
  
  

<div>
<p>Hi James,</p><p>do you want to achieve something in compile-time, which is currently not possible in compile-time, and should be.</p><p>Or do you want to achieve it in the same way, as is currently done or possible in runtime? To have identical code?</p><p> </p><p>The question of UB is a central one:</p><p>If it can be legally used in runtime, it is no UB (by definition).</p><p>You brought up adding two signed integers. Signed Overflow is UB, but signed addition itself (without overflow) is okay.</p><p> </p><p>If it just works with current compilers, but is illegal, it is UB.</p><p> </p><p>However,</p><p>there are legal cases for reinterpret_cast for runtime code, but it is forbidden in compile-time code.</p><p>So you may have a point or use case, which should be made possible in compile-time code.</p><p> </p><p>There are also illegal cases of reinterpret_cast &amp; Co., which are often used and sometimes work in runtime code by pure luck.</p><p> </p><p>However, the optimizers get better and better</p><p>[and we will have LLM AIs in the future in the IDE and in the Optimizier, which suggest many correct and not-correct code rewrites/optimizations and a proof engine will proof whether, the rewritten code is correct, as long as no UB happens; the fastest will actually be compiled or the nicest looking will be kept in the IDE]</p><p>and UB code will have less and less luck.</p><p> </p><p>&quot;Compilers are smart, they can handle it.&quot;</p><p>Just be careful that they do not outsmart you as programmer.</p><p>The most difficult problem for programming is to exactly state, what you mean. UB is not even a valid sentence (program) at all.</p><p> </p><p>Also C++ as a whole goes more and more in the direction of safety and safety profiles. Anything relying on current runtime hacks could also be difficult to compile with activated safety profile in the future.</p><p> </p><p>Best,</p><p>Sebastian<br> </p><blockquote style="border-left:2px solid rgb(50,95,186);padding-left:5px;margin-left:5px">-----Ursprüngliche Nachricht-----<br><strong>Von:</strong>	James via Std-Proposals &lt;<a href="mailto:std-proposals@lists.isocpp.org" target="_blank">std-proposals@lists.isocpp.org</a>&gt;<br><strong>Gesendet:</strong>	Sa 30.11.2024 18:57<br><strong>Betreff:</strong>	Re: [std-proposals] std::typeless_memory (type punning)<br><strong>An:</strong>	Jonathan Wakely &lt;<a href="mailto:cxx@kayari.org" target="_blank">cxx@kayari.org</a>&gt;; <br><strong>CC:</strong>	James &lt;<a href="mailto:james.business.84@gmail.com" target="_blank">james.business.84@gmail.com</a>&gt;; <a href="mailto:std-proposals@lists.isocpp.org" target="_blank">std-proposals@lists.isocpp.org</a>; <br>  <div dir="ltr">Well, my use cases involve having a blob of memory store some types on demand. One part is already mentioned via a paper linked in this discussion: uninitialized memory at compile time for stuff like inplace_vector. The other use case is polymorphism with making use of a fixed amount of memory. Specifically I use it for an alternative implementation of the std::function which is Sesomething else that cannot be used at compile time.<div>The paper linked covers the first use case, but not the second one. <br>Note: I want flexibility of how you wanna guarantee memory used appropriately, so I don&#39;t want the new standard type to have checks for that. There is a lot of examples of &quot;in compile time, compiler won&#39;t allow UB, but in runtime you can do whatever you want&quot;</div></div><br><div><div dir="ltr">On Sat, Nov 30, 2024 at 8:40 PM Jonathan Wakely &lt;<a href="mailto:cxx@kayari.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">cxx@kayari.org</a>&gt; wrote:</div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"> </div><br><div><div dir="ltr">On Sat, 30 Nov 2024 at 17:30, James via Std-Proposals &lt;<a href="mailto:std-proposals@lists.isocpp.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">std-proposals@lists.isocpp.org</a>&gt; wrote:</div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Well, I can&#39;t say anything about being implementation defined or not. What I can say is it&#39;s currently impossible, so for this to work either standard needs to expand or make it implementation defined. <br>std::bit_cast creates copies which is not the same thing. <br>My main issue with the current state of the language is, you can do these things at runtime.</div></blockquote><div> </div><div>What things? You haven&#39;t actually given an example of what you&#39;re doing at runtime and want to do at compile-time. Most type punning is not actually valid C++, although it depends exactly what you mean by type punning.</div><div> </div><div> </div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I have various constructs that make use of type punning, but I can&#39;t use any of them at compile time. I need to have an alternative that solves the same problem to be able to do it in compile time. Even then behaviour isn&#39;t exactly the same.<br><br>Regarding UB, compilers have all the information about literally anything when it&#39;s executed at compile time. Compilers are smart, they can handle it.</div><br><div><div dir="ltr">On Sat, Nov 30, 2024 at 1:45 PM Sebastian Wittmeier via Std-Proposals &lt;<a href="mailto:std-proposals@lists.isocpp.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">std-proposals@lists.isocpp.org</a>&gt; wrote:</div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><p>The current typeless memory is either</p><p> - std::byte, char, unsigned char</p><p> </p><p>As compile-time evaluation tries to avoid UB nearly completely and also tries to not use any implementation-defined features, there is less room for low-level inspection or modification.</p><p> </p><p>There is also constexpr std::bit_cast</p><p> </p><p>What usage examples do you have for the typeless_memory, which are not implementation-defined, allow the compiler to check and avoid UB (which e.g. entails an understanding of object lifetimes, types and memory locations) and which cannot be solved with the current tools in the standard library?</p><p><br> </p><blockquote style="border-left:2px solid rgb(50,95,186);padding-left:5px;margin-left:5px">-----Ursprüngliche Nachricht-----<br><b>Von:</b>	James via Std-Proposals &lt;<a href="mailto:std-proposals@lists.isocpp.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">std-proposals@lists.isocpp.org</a>&gt;<br><b>Gesendet:</b>	Sa 30.11.2024 10:56<br><b>Betreff:</b>	[std-proposals] std::typeless_memory (type punning)<br><b>An:</b>	<a href="mailto:std-proposals@lists.isocpp.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">std-proposals@lists.isocpp.org</a>; <br><b>CC:</b>	James &lt;<a href="mailto:james.business.84@gmail.com" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">james.business.84@gmail.com</a>&gt;; <br>  <div dir="ltr">Currently you can do whatever you want at runtime when it comes to type punning. Sure, all of them might not be safe, but you have some ways to do it safely. However in compile time (as far as I know) there is no way to achieve type punning.<div><br>So I&#39;d like to see this type, in standard library<br><a href="https://godbolt.org/z/1dEjYW1hW" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">https://godbolt.org/z/1dEjYW1hW</a><br><br>It&#39;s only purpose is to allow treating some underlying memory as whatever type you want in compile time without using extra memory. It would also provide a shortcut for runtime usage<br>Currently you can&#39;t achieve that due to placement new and reinterpret_cast not being usable in compile time context</div></div> <pre>--   Std-Proposals mailing list  <a href="mailto:Std-Proposals@lists.isocpp.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">Std-Proposals@lists.isocpp.org</a>  <a href="https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals</a>  </pre>  </blockquote></div> -- <br> Std-Proposals mailing list<br> <a href="mailto:Std-Proposals@lists.isocpp.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">Std-Proposals@lists.isocpp.org</a><br> <a href="https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals" rel="noreferrer" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals</a></div></blockquote></div> -- <br> Std-Proposals mailing list<br> <a href="mailto:Std-Proposals@lists.isocpp.org" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">Std-Proposals@lists.isocpp.org</a><br> <a href="https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals" rel="noreferrer" title="Dieser externe Link wird in einem neuen Fenster geöffnet" target="_blank">https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals</a><br> </blockquote></div></div> </blockquote></div> <pre>-- 
 Std-Proposals mailing list
 <a href="mailto:Std-Proposals@lists.isocpp.org" target="_blank">Std-Proposals@lists.isocpp.org</a>
 <a href="https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals" target="_blank">https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals</a>
 </pre>  </blockquote>
</div>
-- <br>
Std-Proposals mailing list<br>
<a href="mailto:Std-Proposals@lists.isocpp.org" target="_blank">Std-Proposals@lists.isocpp.org</a><br>
<a href="https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals" rel="noreferrer" target="_blank">https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals</a><br>
</div></blockquote></div>

