<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 27, 2016, at 3:35 PM, Dave Abrahams &lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">Casting from a raw pointer to a typed pointer is only more dangerous<br class="">than other raw pointer operations because it is the first step in this<br class="">sequence of operations, which is undefined:<br class=""><br class="">ptrA = rawPtr.cast(to: UnsafePointer&lt;A&gt;.self)<br class="">ptrA.initialize(with: A())<br class="">ptrA.deinitialize()<br class=""><br class="">ptrB = rawPtr.cast(to: UnsafePointer&lt;B&gt;.self)<br class="">ptrB.initialize(with: B())<br class=""></blockquote><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">But it's trivial to get undefined behavior without any of that. &nbsp;Just:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">&nbsp;_ = rawPtr.load(UnsafePointer&lt;NonTrivialType&gt;.self)</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></div></blockquote></div><br class=""><div class=""><div class="">That's another way to obtain a typed pointer, but by itself it is well defined.</div><div class=""><br class=""></div><div class="">This is an important point, so I want to make sure I’m getting it across.</div><div class=""><br class=""></div><div class="">The following code is well-defined:</div><div class="">```</div><div class="">ptrA = rawPtr.initialize(with: A())</div><div class="">ptrA.deinitialize()</div><div class="">ptrB = rawPtr.initialize(with: B())</div><div class="">```</div><div class="">The following code is undefined:</div><div class="">```</div><div class="">ptrA = rawPtr.cast(to: UnsafePointer&lt;A&gt;.self)</div><div class="">ptrA.initialize(with: A())</div><div class="">ptrA.deinitialize()</div><div class="">ptrB = rawPtr.cast(to: UnsafePointer&lt;B&gt;.self)</div><div class="">ptrB.initialize(with: B())</div><div class="">```</div><div class="">It is hard to spot the difference between the two styles without drawing attention to the unsafe cast.</div><div class=""><br class=""></div><div class="">I considered naming the cast `UnsafeRawPointer.bind&lt;T&gt;(to: T.Type)` to indicate that the allocated memory is being bound to a type for the entire duration of its allocation. But it's actually the call to `initialize` a typed pointer that binds the type.</div></div><div class=""><br class=""></div><div class="">-Andy</div></body></html>