1

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?

3
  • Use a pointer of reference in swift? Commented Oct 12 at 4:49
  • Does anything change if you change the parameter to take a pointer and change cppStruct to a pointer? Commented Oct 13 at 12:40
  • Thank you, @PepijnKramer and @DavidG. That is a usable workaround, though involves some extra steps. I could not use UnsafePointer<CppStruct> as parameter type, so used UnsafeRawPointer and then bound the memory to CppStruct. I'm still wondering why a method is not visible in C++ if a parameter is of a non-copyable type or Unsafe(Mutable)Pointer<T>, where T may or may not be non-copyable. Commented Oct 13 at 19:30

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.