Re: Mostly Harmless: c++bookends - 2 of 4

From:Kurt Harriman <harriman(at)acm(dot)org>
To:pgsql-hackers(at)postgresql(dot)org
Subject:Re: Mostly Harmless: c++bookends - 2 of 4
Date:2008-12-05 09:16:37
Message-ID:[email protected]
Views:Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists:pgsql-hackers

(Re-sending just the second of four es: c++bookends)

These es are based on CVS head in which the latest commit was
user: petere
date: Thu Dec 04 17:51:28 2008 +0000
summary: Default values for function arguments

2. c++bookends

C++ code can call C functions and share global variables with C,
provided those declarations are surrounded by "bookends":

extern "C" {
...
};

Header files can be made bilingual, to declare interfaces which
look the same to both C and C++ callers. This is done by
placing C++ bookends within the header file, guarded by #ifdefs

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}; /* extern "C" */
#endif

This way the C++ caller can just #include the header file without
worrying whether the interface is implemented in C or C++.

Usually, extension modules written in C++ will put bookends around
all of their PostgreSQL #includes.

However, "postgres.h" usually stands alone as the first #include,
followed by some system #includes, and then the rest of the
PostgreSQL #includes. It is much nicer if a C++ file has just one
pair of bookends around its main block of PostgreSQL #includes.
This gives postgres.h its own internal bookends, making it
bilingual, so that its #include can continue to stand alone at the
head of each file.

Just a few additional header files are mentioned in the PostgreSQL
Reference Manual for add-on developers to use: fmgr.h, funcapi.h,
and spi.h. This adds bookends within those three files for
the benefit of beginners writing very simple extensions in C++.
Documentation and learning are simplified because C example code
can be compiled as C or C++ without change.

AttachmentContent-TypeSize
c++bookends.text/plain2.0 KB

In response to

Responses

Browse pgsql-hackers by date

 FromDateSubject
Next MessageKurt Harriman2008-12-05 09:18:46Re: Mostly Harmless: c++configure - 3 of 4
Previous MessageKurt Harriman2008-12-05 09:13:37Re: Mostly Harmless: c++reserved - 1 of 4