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
Now that `SharedObject` is imported as a reference type in Swift, the programmer will be able to use it in the following manner:
1252
1252
```swift
1253
-
// The C++ constructor is imported as a Swift initializer
1254
1253
let object1 = SharedObject.create()
1255
-
let object2 = SharedObject()
1254
+
let object2 = SharedObject() // The C++ constructor is imported as a Swift initializer
1256
1255
object1.doSomething()
1257
1256
object2.doSomething()
1258
1257
// `object` will be released here.
1259
1258
```
1260
1259
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
1262
1261
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++.
1264
1265
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.
In this case, Swift will import the static `make` function as a Swift initializer:
1279
1278
1280
1279
```swift
1281
-
let object = SharedObject(id: 42)
1282
-
object.doSomething()
1280
+
let object = SharedObject(42)
1283
1281
```
1284
1282
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.
0 commit comments