ABI 界限上的可攜性 (現代 C++)
使用完全可移植的型別和 在二進位介面 (ABI) 界限的慣例。 「可攜式型別」是 C++. 內建的型別或只包含 C 內建型別的結構。 類別型別當呼叫端和被呼叫端對配置、呼叫慣例等達成協議時才能使用。當兩個使用相同編譯器編譯和編譯器設定時,才有可能。
如何簡化 C 可攜性的類別
當呼叫端可能會被其他編譯器/語言編譯時,那麼以特定呼叫慣例「簡化」至「外部 C」API:
// class widget {
// widget();
// ~widget();
// double method( int, gadget& );
// };
extern “C” { // functions using explicit “this”
struct widget; // + opaque type (fwd decl only)
widget* STDCALL widget_create(); // ctor → create new “this”
void STDCALL widget_destroy( widget* ); // dtor → consume “this”
double STDCALL widget_method( widget*, int, gadget* ); // method → use “this”
}