C++ support in Swift

Surprisingly I haven’t seen a pitch or topic on c++ support in Swift in the forums (post a link in the comments if I missed it). I want to open up a pitch on adding c++ support for Swift. This would be great to think about early on to get the implementation right as c++ templates will be the most tricky part. This definitely wouldn’t make it for Swift 5 (Haha) but either way I want to discuss it. If we are not supporting this ever can we get better context into why it isn’t or what is the work needed.

Pitch:
Add a calling convention from c++ into Swift. In other words allow Swift to import and call c++ code directly in Swift itself. I don’t believe mixing the languages into one source file is necessary (ex: Objective-C++).

Ex:

// Foo.swift
Import MyCppClass

Class Example {
   func callToCpp() {
     MyCppClass().bar() // this example is very naive and simple but you get the idea. 
   }
}

I also want to bring up the point of why this is needed. Swift to become more cross-platform (Linux, Windows) it will reach situations where it will be needed to play friendly with larger code bases that can built with many different languages and c++ being the most common in cross platform. Another desire for c++ compatibility is to allow Swift to become an easier choice when deciding to choose it as a modern day coding language. This will become more attractive for developers to think in Swift first while being able to leverage other technologies such as c++.

6 Likes

See here: C++ / Objective-C++ Interop - #2 by Douglas_Gregor

In short, C++ interop is a massive undertaking which the core team has given some thought to.

5 Likes

Basically, any data structure which simultaneously always guarantees 'n lg n' or better performance and always maintains sort order is not implemented natively in Swift or any other language which Swift currently supports in interop. But it does exist in vanilla C++. Like std::map. Not having support in Swift for C++ interop will cause its slow demise, so please implement soon lol. Vanilla C++ is my strongest language.