Hello ladies and gentlemen. First of all, I would like to thank everyone who made it possible to interoperate Swift and C++ code - it's a gigantic work and you did it great. I'm using this feature since day zero and I was able to build my Swift 6 Frameworks the way that the client code won't notice if the api comes from a C++ or Swift code. I even made a Swift global actor with custom executor that schedules jobs on my C++ task manager's specific queue and it works fabulous .
Even though there are still some issues, for the most of them I've written workarounds, and it's still way better than not having the Swift-C++ interoperability feature at all. There is one issue that recently bothered me the most. Let's suppose I have a 3d vector struct:
struct vector3f {
union {
float contents[3];
struct {
float x;
float y;
float z;
};
};
vector3f(): x(0), y(0), z(0) { }
};
And I have a 3d box struct:
struct box3f {
union {
struct {
vector3f start;
vector3f end;
};
struct {
float left;
float bottom;
float back;
float right;
float top;
float front;
};
};
box3f(): start(), end() { }
};
The code is simplified for example purposes. You could notice that I use unions to conveniently access vector's and boxes members. In practice, I have unit tests, paddings and many other things to make sure that nothing is misaligned and properly accessed.
If I access box's members from the Swift code, everything compiles as expected:
let box = box3f()
let left = box.left
Then I noticed while working with vector3f that the vector is not copied correctly - in my case, the x value of copy became 0, y became original's x and z became original's y. I didn't think about that much and decided to write my own copy constructor for vector3f:
struct vector3f {
// ...
vector3f(const vector3f& other): x(other.x), y(other.y), z(other.z) { }
};
Now every instance of vector3f copies correctly, but then I have a problem with box3f:
Record 'box3f' is not automatically available: does not have a copy constructor or destructor; does this type have reference semantics?
So I've also implemented a copy constructor for box3f:
struct box3f {
// ...
box3f(const box3f& other): start(other.start), end(other.end) { }
};
And now I get a slap in the face from the Swift compiler if I try to access box's left or right property from Swift:
Command SwiftCompile failed with a nonzero exit code
Looks like Swift compiler takes user defined copy constructors too personal and just crashes. The only solution I see right now is to write getter and setter functions from C++ code. Does anyone have the similar issue?