What syntax will allow C++ Standard Library statements work with CxxStdlib?

I am trying to figure out details of the syntax of Swift statements that utilize the C++ Standard Library. I understand the C++ Standard Library can be used in Swift if one first sets the compiler language interoperability setting to ā€œC++/Objective-Cā€ and then begins with the import CxxStdlib statement.

There is a cross-reference of Swift and C++ data types at: Swift.org - Supported Features and Constraints of C++ Interoperability but not all those C++ types worked for me when I tried them. Some examples are below.

Below are my tests of several of the data types along with the error messages I am getting.

import CxxStdlib

let s0: std.string = "some_text" // works ok
let i0: std.int32_t = 5 // works ok
let i1: std.int32_t = 5 // works ok
let d0: std.float = 5.5 // 'float' is not a member type of enum
let d0: std.double = 5.5 // 'double' is not a member of type enum
let v0: std.vector // 'Consecutive statements on a line must be separated by a ;

What syntax do I need to use to make functional the ones that do not work?

For std.vector specifically, it needs a template argument to work, but IIRC you have to specialise that template on the C++ side before you can use it on the Swift side.

For the rest of them, float and double in C++ aren't structs unlike they are in Swift, they aren't defined in the std namespace either. std.string is a class though, which makes it available, while int variants are basically type aliases, both declared in the std namespace. Just use Swift's Float and Double, I'd expect both of those to bridge well to corresponding C++ types without a need to refer to their C++ namesakes on the Swift side somehow.

As a side note, this inconsistency in how C++ declares its basic types illustrates the comparative elegance of it in Swift. In the latter all basic types are just structs in the stdlib.

1 Like