Skip to content

Commit 62fa2a0

Browse files
committed
[interop][docs] add language markers to some doc snippets in reverse interop user guide
1 parent c5a617a commit 62fa2a0

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

docs/CppInteroperability/UserGuide-CallingSwiftFromC++.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Importing Swift APIs into C++ requires certain C++ features introduced in newer
3636

3737
A Swift module can be imported over into C++ by using an `#include` that imports the generated C++ header for that module:
3838

39-
```
39+
```swift
4040
// Swift module 'MyModule'
4141
func myFunction();
4242

@@ -46,7 +46,7 @@ func myFunction();
4646

4747
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++:
4848

49-
```
49+
```c++
5050
// C++
5151
#include "MyModule-Swift.h"
5252

@@ -87,10 +87,12 @@ Fundamental primitive types have a C++ fundamental type that represents them in
8787

8888
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.
8989

90-
```
90+
```swift
9191
// Swift module 'MyModule'
9292
func myFunction(x: float, _ c: Int) -> Bool
93+
```
9394

95+
```c++
9496
// C++
9597
#include "MyModule-Swift.h"
9698

@@ -103,10 +105,12 @@ int main() {
103105

104106
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:
105107

106-
```
108+
```swift
107109
// Swift module 'MyModule'
108110
func swapTwoInts(_ a: inout Int, _ b: inout Int)
111+
```
109112

113+
```c++
110114
// C++ interface snippet
111115
void swapTwoInts(swift::Int &a, swift::Int &b) noexcept;
112116

@@ -123,7 +127,7 @@ void testSwap() {
123127
124128
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:
125129
126-
```
130+
```swift
127131
func greet(person: String, in city: String) {
128132
print("Hello \(person)! Welcome to \(city)!")
129133
}
@@ -137,7 +141,7 @@ greet(person: "Bill", from: "San Jose") // calls the second overload of greet.
137141

138142
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:
139143

140-
```
144+
```swift
141145
@expose(C++, greetPersonIn)
142146
func greet(person: String, in city: String) {
143147
print("Hello \(person)! Welcome to \(city)!")
@@ -153,11 +157,13 @@ func greet(person: String, from hometown: String) {
153157

154158
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:
155159

156-
```
160+
```swift
157161
// Swift module 'MyModule'
158162
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
159163
}
164+
```
160165

166+
```c++
161167
// C++ interface snippet
162168
void someFunction(swift::Int parameterWithoutDefault, swift::Int parameterWithDefault = 12) noexcept;
163169

@@ -182,15 +188,15 @@ Swift default parameter values that are set to a `#file` or `#line` call site sp
182188
183189
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:
184190
185-
```
191+
```swift
186192
func arithmeticMean(_ numbers: Double...) -> Double {
187193
...
188194
}
189195
```
190196

191197
can be called from C++ using a C++ initializer list:
192198

193-
```
199+
```c++
194200
arithmeticMean({ 1.0, 2.0 });
195201
```
196202

0 commit comments

Comments
 (0)