Static inline functions with static local variables

M

Martin Wells

C89 doesn't have inline but I wanted to use inline in my C89-compliant
program. Someone suggested to me that I use macros to determine
whether inline was supported, and if necessary, simply define it like:

#define inline /* nothing */

, and then define the functions within header files with static
inline:

static inline void Func(void) {}

Something was nagging at me though, some reason why the static-ness of
the function would make it somehow different from plain old inline.
When total it finally occured to me:

static inline void Func(void)
{
static int count = 0;
}

*With* static, each translation unit has its own "count". WithOUT
static, there's only one "count" for the entire program.

Anyway just thought I'd share. . .

Martin
 
S

santosh

Martin said:
C89 doesn't have inline but I wanted to use inline in my C89-compliant
program. Someone suggested to me that I use macros to determine
whether inline was supported, and if necessary, simply define it like:

#define inline /* nothing */

, and then define the functions within header files with static
inline:

static inline void Func(void) {}

Something was nagging at me though, some reason why the static-ness of
the function would make it somehow different from plain old inline.
When total it finally occured to me:

static inline void Func(void)
{
static int count = 0;
}

*With* static, each translation unit has its own "count". WithOUT
static, there's only one "count" for the entire program.

Anyway just thought I'd share. . .
From n1256.pdf:

6.7.4 (3):

An inline de nition of a function with external linkage shall not
contain a de nition of a
modi able object with static storage duration, and shall not contain a
reference to an
identi er with internal linkage.
 
A

Army1987

6.7.4 (3):

An inline de nition of a function with external linkage shall not
contain a de nition of a
modi able object with static storage duration, and shall not contain a
reference to an
identi er with internal linkage.
I see that they haven't stopped using the one-character fi
ligature, which what's more got lost in the way of your post from
you to me.
 
M

Martin Wells

santosh:
6.7.4 (3):

An inline definition of a function with external linkage shall not
contain a defnition of a
modifable object with static storage duration, and shall not contain a
reference to an
identifer with internal linkage


Ah I see. C++ differs from C in that you can have static-duration
objects within an extern inline function.

Martin
 
S

santosh

Army1987 said:
I see that they haven't stopped using the one-character fi
ligature, which what's more got lost in the way of your post from
you to me.

Yes. It was displayed fine in my newsreader's editor. Guess I'll have to
manually replace them. It would be nice if WG14 could provide a HTML
version of their drafts, if not all then at least important ones like
n1256.pdf.
 
C

CBFalconer

Army1987 said:
I see that they haven't stopped using the one-character fi
ligature, which what's more got lost in the way of your post from
you to me.

That, to my knowledge, is peculiar to the Foxread PDF displayer.
 
C

CBFalconer

santosh said:
.... snip ...

Yes. It was displayed fine in my newsreader's editor. Guess I'll
have to manually replace them. It would be nice if WG14 could
provide a HTML version of their drafts, if not all then at least
important ones like n1256.pdf.

Not HTML, but pure text. Text is universal, and can be handled by
such things as grep and a multitude of searching mechanisms.
 
K

Keith Thompson

Army1987 said:
I see that they haven't stopped using the one-character fi
ligature, which what's more got lost in the way of your post from
you to me.

Copy-and-pasting directly from the same document, I get:

An inline definition of a function with external linkage shall not contain a definition of a
modifiable object with static storage duration, and shall not contain a reference to an
identifier with internal linkage.

Indenting and re-wrapping produces:

An inline definition of a function with external linkage shall not
contain a definition of a modifiable object with static storage
duration, and shall not contain a reference to an identifier with
internal linkage.

I can see the fi ligatures when I zoom in on the PDF, but apparently
my PDF viewer (or something) is smart enough to translate them to
"fi". Apparently it depends on the software you use to view the
document.

I'm using Adobe Reader 8.1.0 under Windows XP, with copy-and-paste
into an xterm window running under Cygwin. I get the ligatures when I
copy-and-paste into OpenOffice Writer, but then I get ligatures when I
type "fi" manually.
 
R

Richard Heathfield

CBFalconer said:
Not HTML, but pure text. Text is universal, and can be handled by
such things as grep and a multitude of searching mechanisms.

On the other hand, text has presentation issues when faced with subscripts,
superscripts, and especially equations. HTML deals with some of these, and
can hand off the rest onto PNGs.
 
S

santosh

Richard said:
CBFalconer said:


On the other hand, text has presentation issues when faced with
subscripts, superscripts, and especially equations. HTML deals with
some of these, and can hand off the rest onto PNGs.

Also the contents, forward references and index could be in the form of
links, which would, IMHO, be of great convenience. If someone really
want plain text, there are good tools to extract it from the HTML,
unlike extracting text from PDF.
 
A

Army1987

Not HTML, but pure text. Text is universal, and can be handled by
such things as grep and a multitude of searching mechanisms.
Stuff like "15 nesting levels for <code>#include</code>d files"
look particularly silly if #include and d have the same font.
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top