Skip to content

Commit 0e6bb12

Browse files
committed
Addressed some comments from j-hui
1 parent d6c8456 commit 0e6bb12

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

documentation/cxx-interop/index.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,12 @@ object.doSomething()
12551255
// `object` will be released here.
12561256
```
12571257

1258-
### Calling conventions when passing shared reference types from Swift to C++
1259-
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, and retains the ownership of the value. In other words, the argument is passed at `+0` and there is no transfer of ownership.
1260-
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.
1258+
### Calling conventions when passing Shared Reference Types from Swift to C++
1259+
1260+
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, and retains the ownership of the value.
1261+
In other words, the argument is passed at `+0` and there is no transfer of ownership.
1262+
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.
1263+
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.
12611264

12621265
```swift
12631266
var obj = SharedObject.create()
@@ -1272,16 +1275,21 @@ void receiveSharedObject(SharedObject *sobj) {
12721275
```
12731276
12741277
Note that if the argument is an inout (non-const reference) as shown below:
1278+
12751279
```c++
12761280
void takeSharedObjectAsInout(SharedObject *& x) { ... }
12771281
```
12781282

12791283
which would be imported in Swift as
1284+
12801285
```swift
12811286
func takeSharedObjectAsInout(_ x: inout SharedObject) { ... }
12821287
```
12831288

1284-
the C++ function can overwrite the value of the argument with the new value. 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. 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.
1289+
The C++ function can overwrite the value of the argument with the new value.
1290+
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.
1291+
Adhering to these rules is necessary to safely and correctly pass around `SWIFT_SHARED_REFERENCE` between Swift and C++.
1292+
These rules are also generally recommended conventions to manage shared objects that use reference counting.
12851293

12861294
### Inheritance and Virtual Member Functions
12871295

0 commit comments

Comments
 (0)