How developed is C++ interoperability?

The short answer is: it's not yet ready. Depending on your library, you might be OK, but there's no guarantee, and I wouldn't be surprised if you run into some crashes, or linker errors. (In fact, I would be surprised if you don't.) Not to mention, many things may (and probably will) change in the next few months, so I wouldn't sink a bunch of work into a project just yet.

Here's a few important things that we do/don't support:

  • We have very rudimentary support for function templates.
  • Class templates should land in the next few days/weeks.
  • Fully specialized class templates are supported.
  • C++ constructors are supported.
  • C++ copy constructors/destructors should land in the next few days/weeks.
  • Move constructors are not supported (and won't be for some time, probably).
  • We don't yet support dependent types (those may be coming soon, though).
  • We can't yet import the C++ standard library.
  • Namespaces and other nested nominal types are mostly supported, some bug fixes for these should land in the next few days.
  • System C headers / Glibc support is finicky on Linux (I think C++ support will work best on macOS at this stage).

If you want to continue anyway... here are some suggestions:

  • Make sure to use a ToT build. Every few days we're submitting new fixes and improvements.
  • Write plain code; don't use templates; if possible create "C-like" bridge functions.
  • As Dan said, you can use -Xswiftc -enable-cxx-interop to enable C++, but this will implicitly disable C/Objective-C interop. So be careful that you're not using any of those libraries (such as Foundation).

Hope this helps.

11 Likes