Skip to content

Commit d332568

Browse files
committed
edits
1 parent 3100aea commit d332568

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

documentation/cxx-interop/index.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,26 +1250,25 @@ 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-
// The C++ constructor is imported as a Swift initializer
12541253
let object1 = SharedObject.create()
1255-
let object2 = SharedObject()
1254+
let object2 = SharedObject() // The C++ constructor is imported as a Swift initializer
12561255
object1.doSomething()
12571256
object2.doSomething()
12581257
// `object` will be released here.
12591258
```
12601259

1261-
You can create instances of `SharedObject` directly from Swift by calling its C++ constructor through a Swift initializer.
1260+
#### Constructing objects of Shared Reference Types from Swift
12621261

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++.
1262+
As demonstrated in the provided example, starting from Swift 6.2, you can create instances of `SWIFT_SHARED_REFERENCE` types by invoking their initializer.
1263+
Note that the Swift compiler uses the default `new` operator to construct C++ shared reference types.
1264+
If you want to hide the constructors of shared reference types, you can also delete the default operator `new` in C++.
12641265

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:
1266+
You can also import a user-defined C++ static factory function as a Swift initializer by annotating it with `SWIFT_NAME("init(…)")` annotation macro, ensuring that the number of underscore placeholders matches the number of parameters in the factory function.
1267+
For example:
12691268

12701269
```cpp
12711270
struct SharedObject {
1272-
static SharedObject* make(int id) SWIFT_NAME("init(_:)");
1271+
static SharedObject* make(int id) SWIFT_NAME("init(_:)");
12731272

12741273
void doSomething();
12751274
} SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject);
@@ -1278,12 +1277,11 @@ static SharedObject* make(int id) SWIFT_NAME("init(_:)");
12781277
In this case, Swift will import the static `make` function as a Swift initializer:
12791278
12801279
```swift
1281-
let object = SharedObject(id: 42)
1282-
object.doSomething()
1280+
let object = SharedObject(42)
12831281
```
12841282

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.
1286-
1283+
Note that if a C++ constructor and a user-annotated static factory (using `SWIFT_NAME`) have identical parameter signatures, Swift favors the static factory when resolving initializer calls.
1284+
This is particularly useful when you want to use a custom allocator or want to disable direct construction entirely and expose only factories.
12871285

12881286
### Inheritance and Virtual Member Functions
12891287

0 commit comments

Comments
 (0)