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
Copy file name to clipboardExpand all lines: docs/CppInteroperability/UserGuide-CallingSwiftFromC++.md
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ Importing Swift APIs into C++ requires certain C++ features introduced in newer
36
36
37
37
A Swift module can be imported over into C++ by using an `#include` that imports the generated C++ header for that module:
38
38
39
-
```
39
+
```swift
40
40
// Swift module 'MyModule'
41
41
funcmyFunction();
42
42
@@ -46,7 +46,7 @@ func myFunction();
46
46
47
47
A C++ namespace is used to represent the Swift module. Namespacing provides a better user experience for accessing APIs from different modules as it encapsulates the different module interfaces in their own namespace. For example, in order to use a Swift module called `MyModule` from C++, you have to go through the `MyModule::` namespace in C++:
48
48
49
-
```
49
+
```c++
50
50
// C++
51
51
#include"MyModule-Swift.h"
52
52
@@ -87,10 +87,12 @@ Fundamental primitive types have a C++ fundamental type that represents them in
87
87
88
88
A function that takes or return primitive Swift types behaves like any other C++ function, and you can pass in the C++ types when calling them, just as you’d expect.
89
89
90
-
```
90
+
```swift
91
91
// Swift module 'MyModule'
92
92
funcmyFunction(x: float, _c: Int) ->Bool
93
+
```
93
94
95
+
```c++
94
96
// C++
95
97
#include"MyModule-Swift.h"
96
98
@@ -103,10 +105,12 @@ int main() {
103
105
104
106
A Swift `inout` parameter is mapped to a C++ reference type in the C++ function signature that’s generated in the C++ interface. You can then pass in a value directly to an `inout` parameter from C++ side, like the example below:
Swift allows you to specify which overload of the function you would like to call using argument labels. For example, the following snippet is explicitly calling the second definition of `greet` because of the call using `greet(person:,from:)` argument labels:
125
129
126
-
```
130
+
```swift
127
131
func greet(person: String, in city: String) {
128
132
print("Hello \(person)! Welcome to \(city)!")
129
133
}
@@ -137,7 +141,7 @@ greet(person: "Bill", from: "San Jose") // calls the second overload of greet.
137
141
138
142
C++ only allows us to select which overload of the function we want to call by using type-based overloading. In cases where type-based overloading isn’t sufficient, like when the arguments have the same type but a different argument label, you can use the `exposed` attribute to provide a different C++ name for the C++ function, like in the following example:
Default parameter values allow you to provide a default value for Swift function parameter, allowing the program to not specify it when calling such function. The generated C++ interface for a Swift function contains default parameter values as well, just like in the following example:
@@ -182,15 +188,15 @@ Swift default parameter values that are set to a `#file` or `#line` call site sp
182
188
183
189
A variadic parameter is a parameter that accepts zero or more values of the specified type. It gets exposed in C++ using a `swift::variadic_parameter_pack` class template. You can pass values to a variadic parameter using the C++ initializer list syntax. For example, the following Swift function with a `Double` variadic parameter:
0 commit comments