Objective C++ 稳定的 ABI?

Objective C++ stable ABI?

问题 1:我读到 Objective-C 底层使用 C ABI?对吗?

问题 2:但在那种情况下,Objective-C 如何轻松地将自身与 C++ 互操作?

我可以用 Objective-C 和 C++ 构建静态库,而 public headers 纯粹是 Objective-C。 问题 3:无论编译器版本如何,合作伙伴都可以在他们的 iOS 应用程序中使用它是否足够稳定?

Question 1: I read that Objective-C under the hood uses the C ABI? Is that correct?

有点。 Objective-C 有自己的链接规则,用于 class 在运行时注册等。不过,函数调用(消息传递)约定确实与 C 的约定相匹配。无论如何,它被认为是稳定的。

Question 2: But in that case how does Objective-C so easily interop itself with C++?

C 和 C++ 也可以非常直接地互操作,所以我想问题是为什么不呢?

编译器需要确保的主要事情是 Objective-C classes 的 C++ 类型成员 (ivars) 正确遵循 C++ constructor/destructor 规则,并且 ARC 指针指向 Objective-C C++ class 中的类型对象在 C++ construction/destruction 上正确执行 ARC retain/release 操作。这些方面确实可以正常工作。

Question 3: Is this stable enough to be used by partners in their iOS apps irrespective of their compiler versions?

您对 C++ 标准库 (libc++) 的使用必须与库的使用者使用的版本兼容。不过,到目前为止,Apple 已确保这在 OS 和工具链版本中都能正常运行。如果您使用非常现代的标准库功能(例如 std::variant),您可能会发现这限制了可能的部署目标。