Here is a simple C++ structure (member method implementations are trivial and omitted for brevity):
struct SWIFT_NONCOPYABLE CppStruct { CppStruct() {} CppStruct(const CppStruct&); CppStruct(const CppStruct&&); ~CppStruct(); }; Consider a simple Swift class that has a method taking the above C++ struct:
public class SwiftProcessor { public init() { } public func processCppStruct(_ x : borrowing CppStruct) { print("Processing a CppStruct..") } } The Swift method is invoked elsewhere from C++ code:
SwiftProcessor proc = SwiftProcessor::init(); CppStruct cppStruct; proc.processCppStruct(cppStruct); When this C++ code is compiled, the following error is encountered: no member named 'processCppStruct' in 'SwiftProcessor'.
This is on Ubuntu. A similar error is reported on MacOS using Xcode. If SWIFT_NONCOPYABLE is removed, this works fine on both platforms. Swift 6.2 is used on both.
The Swift code won't compile without an ownership specifier. Changing borrowing to consuming or inout doesn't help.
It looks like somehow a non-copyable C++ type cannot be passed from C++ to a Swift method. Any idea why and how to work around this?
UnsafePointer<CppStruct>as parameter type, so usedUnsafeRawPointerand then bound the memory toCppStruct. I'm still wondering why a method is not visible in C++ if a parameter is of a non-copyable type orUnsafe(Mutable)Pointer<T>, whereTmay or may not be non-copyable.