Skip to content

Commit 21f224f

Browse files
committed
addressed review comments
1 parent 1e4fd7e commit 21f224f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

documentation/cxx-interop/index.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,11 +1280,23 @@ owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is
12801280
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
12811281
The C++ caller of this function is responsible for releasing the object.
12821282

1283-
If a C++ Shared Reference Type is passed as an non-const argument to a C++ API from Swift, the Swift compiler *guarantees* that the passed value would be alive. Also Swift *assumes* that the C++ API is not consuming i.e., it returns with a valid object in the passed reference at the end of the C++ API call.
1283+
If a C++ shared reference type is passed as an non-const argument to a C++ API from Swift, the Swift compiler guarantees that the passed value would be alive, and retains the ownership of the value. In other words, the argument is passed as +0 and there is no transfer of ownership.
1284+
The C++ function is responsible for ensuring that the value pointed to by the parameter is alive during and at the end of the call. The C++ function should not assume it has ownership of the value and should do necessary retain operations if it is needs to take ownership.
1285+
If the argument is an inout (non-const reference) as shown below:
1286+
```c++
1287+
void takeSharedObjectAsInout(SharedObject *& x) { ... }
1288+
```
1289+
1290+
which would be imported in Swift as
1291+
```swift
1292+
func takeSharedObjectAsInout(_ x: inout SharedObject) { ... }
1293+
```
1294+
1295+
If the C++ function overwrites the value of the argument with the new value, it 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. Adhering to these rules is necessary to safely and correctly pass around `SWIFT_SHARED_REFERENCE` between Swift and C++. These rules are also generally recommended conventions to manage shared objects that use reference counting.
12841296

12851297
```swift
12861298
var obj = SharedObject.create()
1287-
receiveSharedObject(obj) // Swift gaurantees that obj is alive
1299+
receiveSharedObject(obj) // Swift guarantees that obj is alive
12881300
```
12891301

12901302
```c++

0 commit comments

Comments
 (0)