When writing portable code, you should stick to the standard, but sometimes you have to use compiler-specific extensions of the standard. This appendix lists some of the more interesting extensions in a few compilers. It is not a reference of compiler extensions, but more an illustration of the kinds of extensions that compiler writers choose to implement.
Borland has several extensions to C++ to support its Rapid Application Development products: C++ Builder (for Microsoft Windows) and Kylix (for Linux). This section presents highlights of the RAD extensions.
_ _closure
In C++ Builder, a closure is like a pointer to a member
function that has been bound to a specific object. Given a
closure, you can call it the way you would call an ordinary
function. To declare a closure type or object, use _ _closure
as a modifier for the name of
a function pointer:
typedef int (* _ _closure MemFunc)(int); MemFunc func; struct demo { int sqr(int x) { return x * x; } }; demo d; func = d.sqr; int n = func(10); // n = 100
_ _declspec
The _ _declspec
keyword
takes an attribute in parentheses and serves as a declaration
specifier. Depending on the attribute, it can be used to modify a
function, object, or class. For example, _ _declspec(noreturn)
is a function
specifier that tells the compiler that the function does not
return, which permits additional optimization and error-checking
(for example, eliminating statements that follow a call to the
noreturn
function):
void _ _declspec(noreturn) abort( );
Other attributes include:
thread
A storage-class specifier that declares an object to be local to a thread; that is, each runtime thread has a separate copy of the object.
dllexport
A function specifier that tells the linker to export the function name from a dynamic-link library (DLL).
uuid(
string-literal
)
Modifies a class declaration. It associates a
universally unique identifier (UUID) with the class, which
is required for implementing COM objects in Windows. A
class's UUID can be retrieved with the _ _uuidof
operator.
_ _int64
The _ _int64
type is a
64-bit integer type. In current releases of C++ Builder and Kylix,
long
is 32 bits. A 64-bit
integer literal is written with a suffix of i64
(e.g., 10000000000000i64
).
_ _property
A property is a class member that is used like a data member, but it can have the semantics of a member function. Properties are the foundation for the RAD features of C++ Builder and Kylix. A property is associated with a reader and writer, which can be data member names or member function names:
class TControl { private: int height_; void set_height(int h); . . . _ _published: _ _property int height { read=height_, write=set_height }; }; TControl * ctl = new TControl; ctl->height = 10; // Calls ctl->set_height(10) int h = ctl->height; // Gets ctl->height_
_ _published
The _ _published
access specifier label yields the same
accessibility as the public
keyword, but it also directs the compiler to store additional
runtime type information (RTTI) for the published declarations.
The RAD features use the RTTI when the user designs an
application
_ _thread
_ _uuidof
The _ _uuidof
operator takes an expression as an operand and
returns the UUID of the expression's class. The class declares its
UUID with _ _declspec(uuid(
. .
. ))
. A class can implement the
standard COM member function, QueryInterface
, with _ _uuidof
:
class demo { virtual HRESULT QueryInterface(const UUID& iid, void** obj) { if (iid == _ _uuidof(IUnknown)) { *obj = reinterpret_cast<IUnknown*>(this); static_cast<IUnknown*>(*obj)->AddRef( ); return S_OK; } return E_NOINTERFACE; } };