You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/cxx-interop/index.md
+39-1Lines changed: 39 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1322,6 +1322,44 @@ unOwned.doSomething()
1322
1322
Note that the Swift compiler will automatically infer the ownership conventons for Swift functions returning `SWIFT_SHARED_REFERENCE` types.
1323
1323
See [Exposing C++ Shared Reference Types back from Swift](#exposing-c-shared-reference-types-back-from-swift) for calling Swift functions returning `SWIFT_SHARED_REFERENCE` types from C++.
1324
1324
1325
+
### Calling conventions when passing Shared Reference Types from Swift to C++
1326
+
1327
+
If a C++ shared reference type is passed as an argument to a C++ API from Swift, the Swift compiler guarantees that the passed value would be alive.
1328
+
Swift also retains the ownership of the value.
1329
+
In other words, the argument is passed at `+0` and there is no transfer of ownership.
1330
+
The C++ function should not assume that it has the ownership of the value and should do necessary retain operations if it is needs to take ownership.
1331
+
The C++ function is responsible for ensuring that the value pointed to by the parameter is alive during and at the end of the function call.
1332
+
1333
+
1334
+
```swift
1335
+
var obj = SharedObject.create()
1336
+
receiveSharedObject(obj) // Swift guarantees that obj is alive and it is passed at +0
1337
+
```
1338
+
1339
+
```c++
1340
+
voidreceiveSharedObject(SharedObject *sobj) {
1341
+
...
1342
+
// Swift assumes that sobj is a valid, non-null object at the end of this function
1343
+
}
1344
+
```
1345
+
1346
+
Note that if the argument is an inout (non-const reference) as shown below:
The C++ function can overwrite the value of the argument with the new value.
1359
+
However, the C++ function is responsible for releasing the old value, and ensuring that the new value is properly retained so that the Swift caller has ownership of the new value when the function returns.
1360
+
Adhering to these rules is necessary to safely and correctly pass around `SWIFT_SHARED_REFERENCE` between Swift and C++.
1361
+
These rules are also generally recommended conventions to manage shared objects that use reference counting.
1362
+
1325
1363
### Inheritance and Virtual Member Functions
1326
1364
1327
1365
Similar to value types, casting an instance of a derived reference type to a
@@ -1330,7 +1368,7 @@ base reference type, or vice versa, is not yet supported by Swift.
1330
1368
If a reference type has virtual methods, you can call those methods from Swift.
1331
1369
This includes pure virtual methods.
1332
1370
1333
-
####Exposing C++ Shared Reference Types back from Swift
1371
+
### Exposing C++ Shared Reference Types back from Swift
1334
1372
1335
1373
C++ can call into Swift APIs that take or return C++ Shared Reference Types. Objects of these types are always created on the C++ side,
1336
1374
but their references can be passed back and forth between Swift and C++. This section explains the conventions of incrementing and decrementing
0 commit comments