ABI 边界处的可移植性

在二进制接口边界使用可充分移植的类型和约定。 “可移植类型”是 C 内置类型或只包含 C 内置类型的结构。 类类型只能在调用方和被调用方接受布局、调用约定等时使用。这仅在两者是使用相同的编译器和编译器设置编译时可能。

如何为了 C 可移植性平展类

如果调用方可能是使用另一编译器/语言编译的,则使用特定调用约定“平展”为“外部 C”API

// class widget {
//   widget();
//   ~widget();
//   double method( int, gadget& );
// };
extern "C" {        // functions using explicit "this"
   struct widget;   // opaque type (forward declaration only)
   widget* STDCALL widget_create();      // constructor creates new "this"
   void STDCALL widget_destroy(widget*); // destructor consumes "this"
   double STDCALL widget_method(widget*, int, gadget*); // method uses "this"
}

另请参阅

欢迎回到 C++
C++ 语言参考
C++ 标准库