How to use RE2(C++) in Swift?

I wanted to call RE2 API from C++ in Swift, and I did this:

  1. brew install re2 abseil
  2. Added re2.dyld and library/ header search path to Xcode 15.4.
  3. Added module.modulemap to the include/re2 directory.
module re2 {
  header "re2.h"
  requires cplusplus
  export *
}

And Swift can import re2 for now, but compiler give me this error:

Candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 were provided

in re2/re2/re2.h at main · google/re2 · GitHub

Also the converted re2 swift interface is empty

I'm not very familiar with C++, and I can't find any solution on the internet, can anyone help me?

If I remove most of the contents of the header file it is possible to call it in Swift, the problem is that I don't know what is causing the error.

I suspect the issue is here:

  // Not movable.
  // RE2 objects are thread-safe and logically immutable. You should probably
  // use std::unique_ptr<RE2> instead. Otherwise, consider std::deque<RE2> if
  // direct emplacement into a container is desired. If you really must move,
  // be prepared to submit a design document along with your feature request.
  RE2(RE2&&) = delete;
  RE2& operator=(RE2&&) = delete;

Swift 6 supports non-copyable C++ types (RE2(const RE2&) = delete;) as of Swift 6, but as far as I know it does not support non-movable types because such types cannot currently be represented in Swift (~Copyable structs can still be moved). It’s definitely unfortunate that the clang importer isn’t able to ignore this issue, generate a Swift interface, then mark it as @available(*, unavailable) with an appropriate message, but I’d suspect that issue is why none of the APIs are able to be imported due to them having a dependency on non-movable types in some way.

Edit: The reason the struct is non-movable is because it has stored properties of type absl::once_flag which are also non-movable.

1 Like

Not movable should be the reason, C++'s implicit initialization mentioned by samantha789 is also a missing feature in Swift, which means that it is not feasible to use this API directly.