Skip to content

Commit 3100aea

Browse files
committed
Addressed comments about SWIFT_NAME trick, custom allocator and default operator new
1 parent d61979c commit 3100aea

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

documentation/cxx-interop/index.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,8 +1239,8 @@ To specify that a C++ type is a shared reference type, use the `SWIFT_SHARED_REF
12391239
class SharedObject : IntrusiveReferenceCounted<SharedObject> {
12401240
public:
12411241
SharedObject(const SharedObject &) = delete; // non-copyable
1242-
SharedObject(); // Constructor
1243-
1242+
SharedObject();
1243+
static SharedObject* create();
12441244
void doSomething();
12451245
} SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject);
12461246

@@ -1250,17 +1250,39 @@ void releaseSharedObject(SharedObject *);
12501250
12511251
Now that `SharedObject` is imported as a reference type in Swift, the programmer will be able to use it in the following manner:
12521252
```swift
1253-
// Call the C++ constructor of SharedObject using Swift initializer syntax.
1254-
let object = SharedObject()
1255-
object.doSomething()
1253+
// The C++ constructor is imported as a Swift initializer
1254+
let object1 = SharedObject.create()
1255+
let object2 = SharedObject()
1256+
object1.doSomething()
1257+
object2.doSomething()
12561258
// `object` will be released here.
12571259
```
12581260

1259-
You can create instances of `SharedObject` directly in Swift by calling its C++ constructor through a Swift initializer.
1261+
You can create instances of `SharedObject` directly from Swift by calling its C++ constructor through a Swift initializer.
1262+
1263+
**NOTE:** Swift uses *default* `new` operator for constructing c++ shared reference types. If you'd like to prevent Swift from importing constructors as initializers, you can also delete the *default* `new` operator in C++.
1264+
1265+
1266+
Alternatively, you can also construct instances of `SharedObject` using a user-defined static factory function, provided that the factory function is annotated with `SWIFT_NAME("init(...)")`, where the number of `_` placeholders matches the number of parameters in the factory function.
1267+
1268+
For example, consider a factory that performs custom allocation or returns a singleton instance:
1269+
1270+
```cpp
1271+
struct SharedObject {
1272+
static SharedObject* make(int id) SWIFT_NAME("init(_:)");
1273+
1274+
void doSomething();
1275+
} SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject);
1276+
```
1277+
1278+
In this case, Swift will import the static `make` function as a Swift initializer:
12601279
1261-
Alternatively, you can construct instances using a user-defined static factory function, provided that the factory function is annotated with `SWIFT_NAME("init(...)")`, where the number of `_` placeholders matches the number of parameters in the factory function
1280+
```swift
1281+
let object = SharedObject(id: 42)
1282+
object.doSomething()
1283+
```
12621284

1263-
> **Note**: If a C++ constructor and a user-annotated static factory (via `SWIFT_NAME`) have identical parameter signatures, Swift prefers the static factory when resolving initializer calls.
1285+
**Note**: If a C++ constructor and a user-annotated static factory (via `SWIFT_NAME`) have identical parameter signatures, Swift prefers the static factory when resolving initializer calls. Using `SWIFT_NAME("init(...)")` is especially useful if you want to use a custom allocator or you want to disable direct construction entirely and expose only factories.
12641286

12651287

12661288
### Inheritance and Virtual Member Functions

0 commit comments

Comments
 (0)