UnsafeMutablePointer direct exposure.

I am trying to use the swift type UnsafeMutablePointer directly in C++. According to the documentation mentioned, swift expose this type to C++. But I am not able to use it .

void
GetPointerFromSwift () {
    
// Calls a swift function to get a pointer.
  swift::UnsafeMutablePointer<swit::Int> x = Interop::GetPointer ()

}

Is it not simply:

swift::Int* x = Interop::GetPointer();

?

(That's just a guess, I've never used this. But I think the whole point of "unsafe mutable pointer" is that it's Swift's way of spelling "*".)

Again, I’m not sure what you’re actually asking about here. In general, the C++ spelling for Swift’s UnsafeMutablePointer<X> is X *. And for UnsafePointer<X> it’s const X *.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@DTS Engineer I got your point and i was able to use the pointer in that way.

public func ReturnInteger() -> UnsafeMutablePointer<Int32> {
    
    withUnsafeMutablePointer(to: &value) {pointer in
            return pointer
    }
}
void
CppClass::GetIntegerPointer() noexcept
{
    
    int32_t * x = (int32_t *)Interop::ReturnInteger();

// The above way works  but when i am trying to directly use
// swift::UnsafeMutablePointer<int32_t> x = (int32_t *) Interop::ReturnInteger ();
// it gives build error that UnsafeMutablePointer not found .
 
}

But in the documentation it's mentioned that it is exposed to C++.

Quote

Pointer types, like OpaquePointer, UnsafePointer, UnsafeMutablePointer, UnsafeRawPointer and UnsafeMutableRawPointer

UnQuote

Can you Explain what does this mean?

My reading of that doc is that these types come across to C++ in the natural way. For example, Swift Bool cames across as the C++ bool.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Can you Explain what does this mean?

If you want, file a bug against the docs asking for that page to be explicit about how those pointer types map. The answer, as mentioned above, is that the Swift mutable pointer types map to * and the immutable pointer types map to const *.

@DTS Engineer I know this that swift::Bool is compatible with c++ bool. What I wanted to ask is that in the documentation it is mentioned that Swift exposes UnsafeMutablePointer to C++ (link).

Quote

Pointer types, like OpaquePointer, UnsafePointer, UnsafeMutablePointer, UnsafeRawPointer and UnsafeMutableRawPointer

UnQuote

I directly want to use them in c++ as swift::UnsafeMutablePointer<Int32>.

If it's not true what documentation mean, then what is the manifestation for exposing these UnsafeMutablePointer?

I directly want to use them in c++ as swift::UnsafeMutablePointer<Int32>.

Why?

If it's not true what documentation mean, then what is the manifestation for exposing these UnsafeMutablePointer?

We’ve already answered that, it’s just T*

I guess you could write:

namespace swift {
template <typename T>
using UnsafeMutablePointer = T*;
};
UnsafeMutablePointer direct exposure.
 
 
Q