|
| 1 | +# C++ Interoperability Status |
| 2 | + |
| 3 | +Swift has some experimental ability to interoperate with C++. |
| 4 | +This document provides an overview of the status of the Swift and C++ interoperability support. |
| 5 | + |
| 6 | +## C++ to Swift Interoperability Status |
| 7 | + |
| 8 | +Swift has the experimental ability to import a large subset of C++. |
| 9 | +This section of the document describes which C++ language and standard library features can be imported and used from Swift in an experimental manner. |
| 10 | + |
| 11 | +### Importing C++ |
| 12 | + |
| 13 | +There are currently two experimental ways to import C++ into Swift: |
| 14 | +- **Clang modules**: can be imported into Swift. This requires a module map. |
| 15 | +- **Bridging header**: can be imported into Swift. Headers included in the bridging header will be imported. |
| 16 | +Please note that support for importing C++ 20 modules isn’t implemented. |
| 17 | + |
| 18 | +Both CMake and the Swift package manager can be configured to invoke Swift with the correct arguments to import C++ headers. |
| 19 | + |
| 20 | +**Note**: C++ code is imported using the Objective-C++ language mode on Apple platforms. |
| 21 | + |
| 22 | +### Experimental C++ Language Support |
| 23 | + |
| 24 | +This status table describes which of the following C++ language features can be used in Swift: |
| 25 | + |
| 26 | +| **C++ Language Feature** | **Implemented Experimental Support For Using It In Swift** | |
| 27 | +|---------------------------------------------|---------------------------------------------------------------| |
| 28 | +| Top-level functions | Yes | |
| 29 | +| Enumerations | Yes. That includes `enum class` | |
| 30 | +| Struct / Class types | Yes - as value types, except for types without a copy constructor. Partial experimental support for importing a C++ struct/class as a reference type | |
| 31 | +| Typedefs / Type aliases | Yes | |
| 32 | +| Global Variables | Yes | |
| 33 | +| Namespaces | Yes | |
| 34 | +| Inline Namespaces | Yes, with some known issues (https://bugs.swift.org/browse/SR-15956) | |
| 35 | +| Exceptions | No | |
| 36 | +| Fields | Yes | |
| 37 | +| Member functions | Yes. Some value category overloads aren't imported | |
| 38 | +| Virtual Member Functions | No | |
| 39 | +| Operators | Yes, with some known issues | |
| 40 | +| Subscript Operators | Yes | |
| 41 | +| Constructors | Yes. That includes implicit constructors | |
| 42 | +| Destructor | Yes. C++ destructors are invoked automatically when the value is no longer used in Swift | |
| 43 | +| Copy constructor / copy assignment operator | Yes. Swift invokes the underlying copy constructor when copying a C++ value | |
| 44 | +| Move constructor / move assignment operator | No | |
| 45 | +| Base class member functions / operators | Yes, with some known issues | |
| 46 | +| Function templates | Yes | |
| 47 | +| Class templates | Yes | |
| 48 | +| Dependent types | Partially: imported as Any | |
| 49 | +| Availability Attributes | Yes | |
| 50 | + |
| 51 | + |
| 52 | +The following C++ code patterns or language features have specific mappings to Swift language features when imported in Swift: |
| 53 | + |
| 54 | + |
| 55 | +| **C++ Language Feature** | **Imported Into Swift** | |
| 56 | +|------------------------------------------------------|------------------------------------------------------------------------------------------| |
| 57 | +| `get`/`set` member functions | Imported as computed property (starting from Swift-5.7) | |
| 58 | +| `const`/non-`const` member function overload set | Both overloads are imported as a method, with non-`const` method being renamed to `mutating…` (starting from Swift-5.7). The renaming logic will change in a future version of Swift, and non-`const` methods won't be renamed | |
| 59 | + |
| 60 | + |
| 61 | +Unless stated otherwise (i.e., imported reference types) all Swift features work with imported types. For example: use in generic contexts, protocol conformance, extensions, etc. |
| 62 | + |
| 63 | + |
| 64 | +### C++ Standard Library Support |
| 65 | + |
| 66 | +Parts of libc++ can be imported and used from Swift. |
| 67 | + |
| 68 | +This status table describes which of the following C++ standard library features have some experimental support for using them in Swift. Please note that this is not a comprehensive list and other libc++ APIs that use the above supported C++ language features could be imported into Swift. |
| 69 | + |
| 70 | +| **C++ Standard Library Feature** | **Can Be Used From Swift** | |
| 71 | +|------------------------------------|----------------------------------------------| |
| 72 | +| `std::string` | Yes | |
| 73 | +| `std::vector` | Yes | |
| 74 | + |
| 75 | +## Known Issues |
| 76 | + |
| 77 | +### Inline Namespaces |
| 78 | +- https://bugs.swift.org/browse/SR-15956: Swift's typechecker currently doesn't allow calling a function from an inline namespace when it's referenced through the parent namespace. Example of a test that fails: https://github.com/apple/swift/blob/main/test/Interop/Cxx/namespace/inline-namespace-function-call-broken.swift |
| 79 | + |
| 80 | + |
| 81 | +## Swift to C++ Interoperability Status |
| 82 | + |
| 83 | +This section of the document describes which Swift language and standard library features can be imported and used from C++. |
| 84 | + |
| 85 | +### Importing Swift |
| 86 | + |
| 87 | +Swift has some experimental support for generating a header that can be imported by C++. |
| 88 | + |
| 89 | +### Swift Language Support |
| 90 | + |
| 91 | +This status table describes which of the following Swift language features have some experimental support for using them in C++. |
| 92 | + |
| 93 | +**Functions** |
| 94 | + |
| 95 | +| **Swift Language Feature** | **Implemented Experimental Support For Using It In C++** | |
| 96 | +|--------------------------------|----------------------------------------------------------| |
| 97 | +| Top-level `@_cdecl` functions | Yes | |
| 98 | +| Top-level Swift functions | Partially, only with C compatible types | |
| 99 | +| `inout` parameters | No | |
| 100 | +| Variadic parameters | No | |
| 101 | +| Multiple return values | No | |
0 commit comments