Next: , Previous: Interfacing to C, Up: Interfacing to Other Languages


12.2 Interfacing to C++

The interface to C++ makes use of the following pragmas, which are primarily intended to be constructed automatically using a binding generator tool, although it is possible to construct them by hand.

Using these pragmas it is possible to achieve complete inter-operability between Ada tagged types and C++ class definitions. See Implementation Defined Pragmas, for more details.

pragma CPP_Class ([Entity =>] LOCAL_NAME)
The argument denotes an entity in the current declarative region that is declared as a tagged or untagged record type. It indicates that the type corresponds to an externally declared C++ class type, and is to be laid out the same way that C++ would lay out the type.

Note: Pragma CPP_Class is currently obsolete. It is supported for backward compatibility but its functionality is available using pragma Import with Convention = CPP.

pragma CPP_Constructor ([Entity =>] LOCAL_NAME)
This pragma identifies an imported function (imported in the usual way with pragma Import) as corresponding to a C++ constructor.

A few restrictions are placed on the use of the Access attribute in conjunction with subprograms subject to convention CPP: the attribute may be used neither on primitive operations of a tagged record type with convention CPP, imported or not, nor on subprograms imported with pragma CPP_Constructor.

In addition, C++ exceptions are propagated and can be handled in an others choice of an exception handler. The corresponding Ada occurrence has no message, and the simple name of the exception identity contains ‘Foreign_Exception’. Finalization and awaiting dependent tasks works properly when such foreign exceptions are propagated.

It is also possible to import a C++ exception using the following syntax:

     LOCAL_NAME : exception;
     pragma Import (Cpp,
       [Entity =>] LOCAL_NAME,
       [External_Name =>] static_string_EXPRESSION);

The External_Name is the name of the C++ RTTI symbol. You can then cover a specific C++ exception in an exception handler.