Static inline functions with static local variables

Discussion in 'C Programming' started by Martin Wells, Oct 6, 2007.

  1. Martin Wells

    Martin Wells Guest

    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
    Martin Wells, Oct 6, 2007
    #1
    1. Advertising

  2. Martin Wells

    santosh Guest

    Martin Wells wrote:
    > 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.
    santosh, Oct 6, 2007
    #2
    1. Advertising

  3. Martin Wells

    Army1987 Guest

    On Sat, 06 Oct 2007 05:05:24 -0700, santosh wrote:


    >>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.

    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.
    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Oct 6, 2007
    #3
  4. Martin Wells

    Martin Wells Guest

    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
    Martin Wells, Oct 6, 2007
    #4
  5. Martin Wells

    santosh Guest

    Army1987 wrote:

    > On Sat, 06 Oct 2007 05:05:24 -0700, santosh wrote:
    >
    >
    >>>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.

    >
    > 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.
    santosh, Oct 6, 2007
    #5
  6. Martin Wells

    CBFalconer Guest

    Army1987 wrote:
    > santosh wrote:
    >
    >> 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.

    >
    > 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.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 6, 2007
    #6
  7. Martin Wells

    CBFalconer Guest

    santosh wrote:
    >

    .... 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.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 6, 2007
    #7
  8. Army1987 <> writes:
    > On Sat, 06 Oct 2007 05:05:24 -0700, santosh wrote:
    >>>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.

    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 6, 2007
    #8
  9. CBFalconer said:

    > santosh wrote:
    >>

    > ... 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.


    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.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 7, 2007
    #9
  10. Martin Wells

    santosh Guest

    Richard Heathfield wrote:

    > CBFalconer said:
    >
    >> santosh wrote:
    >>>

    >> ... 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.

    >
    > 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.
    santosh, Oct 7, 2007
    #10
  11. Martin Wells

    Army1987 Guest

    On Sat, 06 Oct 2007 13:10:43 -0400, CBFalconer wrote:

    > santosh wrote:
    >>

    > ... 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.

    Stuff like "15 nesting levels for <code>#include</code>d files"
    look particularly silly if #include and d have the same font.
    :)
    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Oct 8, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Hoffmann
    Replies:
    3
    Views:
    2,813
    Christian Jan├čen
    Aug 8, 2003
  2. Replies:
    3
    Views:
    447
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    1,830
    Kevin Bracey
    Nov 21, 2003
  4. Sean
    Replies:
    4
    Views:
    657
    Rolf Magnus
    Apr 30, 2006
  5. Rahul
    Replies:
    3
    Views:
    442
    James Kanze
    Feb 28, 2008
Loading...

Share This Page