https://forums.swift.org/t/swift-and-c-interoperability-workgroup-announcement/54998 Swift Forums Swift and C++ interoperability workgroup announcement Development Announcements cxx-interop Alex_L (Alex Lorenz) January 31, 2022, 11:35pm #1 Over the past few years there has been a huge amount of interest in bidirectional interoperability between Swift and C++. Thanks to the hard work of zoecarver, egor.zhdan, gribozavr, Michael Forster, Marcel Hlopko, scentini, and many other contributors, the Swift compiler is now able to import and use some C++ APIs, including C++ standard library types like std::string and std::vector . Additionally, a C++ interoperability manifesto that describes the goals and the design for the implementation was written and published on github. To advance the interoperability support between Swift and C++, we are announcing the formation of the Swift and C++ interoperability workgroup as part of the Swift project. This workgroup provides the framework for those interested in Swift and C++ interoperability to work together to refine the interoperability layer's goals and design. Additionally, the workgroup will establish an ongoing discussion about the related Swift compiler changes to implement interoperability support. Workgroup Details * The workgroup is responsible for developing and designing the interoperability model between C++ and Swift. * The workgroup will meet over a video call once a week. * The workgroup will initially focus its discussion on how to rapidly iterate the development of the interoperability model between the two languages. * The workgroup complements the existing Swift evolution process. The workgroup will design the interoperability model first, and then will bring it back for formal evaluation and discussion with the Swift community via the Swift evolution process. Swift Forums A new "C++ interoperability" sub-section of the "Development" section on the forums, open to everyone , will host the relevant technical discussion posts on C++ interoperability. Alongside technical discussion, it will include meeting notes from the workgroup meetings. Get Involved! We would love to hear from anyone interested in joining this workgroup. Those who participate will need to feel comfortable generally meeting the following obligations in terms of commitment: * Dedicate 50 minutes per week that you can use for the interoperability discussions. * Actively participate in the work related to the Swift and C++ interoperability project. This can include things like: + Making code changes in the interoperability layer in the Swift compiler. + Writing user-facing documentation for this feature. + Reviewing related PRs and/or proposals. + Providing design input for mapping specific code patterns between the two languages. Please let us know if you're interested in this effort and if you'd like to join the workgroup by responding to this forum post below. We are planning on forming the workgroup in the next week or two once we have received the responses from this community. The initial set of participants will include the following community members: * Zoe Carver (@zoecarver) * Saleem Abdulrasool (@compnerd) * Ravi Kandhadai Madhavan (@ravikandhadai) * Puyan Lotfi (@plotfi) * Nuri Amari (@nuriamari) * Josh Learn (@guitard0g) * Egor Zhdan (@egor.zhdan) * Dave Abrahams (@dabrahams) * Daniel Rodriguez Troitino (@drodriguez) * Alex Lorenz (@Alex_L ) Alex Lorenz (@Alex_L) will be responsible for setting up the meetings, going through the discussion points, and posting the agenda and the meetings notes on the Swift forums. Thanks, Alex 94 Likes hggz January 31, 2022, 11:53pm #2 Awesome job to everyone involved! The importance and effort of this undertaking can't be overstated. 2 Likes batikan (Batikan) February 1, 2022, 12:47am #3 good news 1 Like VolodymyrBoichentsov (Volodymyr Boichentsov) February 1, 2022, 11:35am #4 :muscle: awesome! 2 Likes zoecarver (Zoe Carver) February 1, 2022, 7:24pm #5 It's awesome to see this getting so much interest! Thanks for the great post, Alex. I've been seeing a lot of questions about C++ interop in response to this post, and I just wanted to use this as an opportunity to answer a couple common ones. That being said, feel free to post any questions here, and I'll do my best to answer them. Is C++ interop supported/shipping? No. C++ interop is not supported (yet). Some things are working (quite well actually), so feel free to try it out, if you're interested. But I want to be very clear: C++ interop is not a supported feature. An experimental version of C++ interop has shipped with most of the recent Swift compilers, however, it is still very unstable, and you will likely see crashes and mis-compiles. In the past few weeks I've been making stability a priority, so future compilers will hopefully be more stable. If you want to try it out, play around with it, test it, etc. you can use -Xfrontend -enable-cxx-interop. But remember, this is not a supported feature, and it is likely to cause crashes and mis compiles. How does this fit with Objective-C/C interop? C++ interop composes nicely with existing Objective-C and C interop. On macOS Objective-C interop is enabled by default (and required) so you're really going to already be using Objective-C++ interop (allowing use of both Objective-C and C++ APIs). On other platforms, C++ interop is pure C++ interop; you can't use Objective-C APIs (at least by default). Does this require any work from the user's side? Not really. By default, you should be able to automatically consume all (or many) of your C++ APIs without any extra work. They will just be automatically imported like Objective-C APIs. That means you don't have to define any bridging functions or API mappings. The two exceptions to this are: 1) you will still have to create either a module map or a bridging header so the compiler knows what headers to import and 2) ideally you will annotate your APIs with _Nullable and _Nonnull attributes (but this is optional). Does interop jump through wrapper functions? No! One of the goals of interop is to provide a fast, native-feeling experience. So, unlike bridging interfaces in most other languages, Swift's C++ interop will import types natively and call functions directly. This means you're getting the absolute maximum performance possible (note: there are a few cases where the compiler has to create a thunk or something, but these are rare, and usually inlined away). Not only is there little to no performance overhead, but in many cases, it's actually faster than going through a C bridging layer. Let's take a common example: bridging std::string. If a user relies on a C++ API that uses std::string, they have to copy the contents of that string into some heap memory and pass that heap memory to Swift through a C bridging layer. Because this is manually allocated heap memory, the Swift program has to remember to deallocate it. So, not only does this introduce an expensive copy, but also introduces unsafety and complexity. C++ interop allows users to get rid of this kind of bridging code. Instead, they can call their C++ API directly, so they don't need to copy their string onto the heap. When the object is done being used, the compiler will invoke string's destructor. Now, the program is safer, faster, and simpler! 8 Likes cabmeurer (Caleb Meurer) February 1, 2022, 9:22pm #6 I would love to be a part of this workgroup to learn and/or contribute as much as I can. Saklad5 (Jeremy Saklad) February 1, 2022, 9:46pm #7 I've been watching your commits recently, and I'm happy to see this moving forward. Being able to use C++ libraries in Swift will be a massive boon for the community, especially if it becomes possible to put C++ source files directly in a Swift package and use it like anything else. C++ doesn't really have a dominant package manager, so that could be a very persuasive way to encourage Swift's adoption. 1 Like * Home * Categories * FAQ/Guidelines * Terms of Service * Privacy Policy Powered by Discourse, best viewed with JavaScript enabled Terms of Service Privacy Policy Cookie Policy