External inline functions calling internal inline functions

Discussion in 'C Programming' started by Daniel Vallstrom, Nov 20, 2003.

  1. Why does c99 require that inline functions with external linkage
    must not refer to functions, possibly inline-specified, with
    internal linkage? Here is the c99 text:

    "6.7.4 Function specifiers
    Syntax
    function-specifier:
    inline

    Constraints
    Function specifiers shall be used only in the declaration of an
    identifier for a function. 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."
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    Here is an example of what one might want to write:

    <start file1.h>
    inline int file1_f( int k );

    <end file1.h>

    <start file1.c>
    static inline int g( int k )
    {
    return k+1;
    }

    inline int file1_f( int k )
    {
    return 2*k + g(k);
    }

    <end file1.c>


    Why isn't this allowed? If the g call in file1_f is inlined
    there is no problem? And if the g call isn't inlined why
    can't the compiler choose to not inline file1_f calls, or
    for that matter choose to inline file1_f calls and handle
    the g call in some way? What's the problem?

    I guess that compilers must give a diagnostic for constraint
    violations but may produce a compiled program.

    To be sure, when I run a program containing something like the
    above example code through gcc and intel's icc, with -std=c99,
    the program compiles just fine. gcc doesn't complain at all but
    IIRC gcc states explicitly that their inline handling doesn't
    follow the standard? icc gives the following warning:

    "warning #1173: an entity with internal linkage cannot be
    referenced within an inline function with external linkage"

    but as said still compiles the program.


    (I thought that this question must have been discussed
    somewhere but I can't find an answer.)


    Daniel Vallstrom
     
    Daniel Vallstrom, Nov 20, 2003
    #1
    1. Advertising

  2. Daniel Vallstrom

    James Kuyper Guest

    (Daniel Vallstrom) wrote in message news:<>...
    > Why does c99 require that inline functions with external linkage
    > must not refer to functions, possibly inline-specified, with
    > internal linkage? Here is the c99 text:
    >
    > "6.7.4 Function specifiers
    > Syntax
    > function-specifier:
    > inline
    >
    > Constraints
    > Function specifiers shall be used only in the declaration of an
    > identifier for a function. 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."
    > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


    The reason is that an inline definition of a function with external
    linkage is meant to be the same function in all translation units,
    whether or not it actually gets inlined. For any given identifier with
    internal linkage, the inline definition of the function which used
    that identifier would be defining a different function in each
    translation unit. If you actually want it to be different in each
    translation unit, it should either not be inline, or not have external
    linkage, depending upon your reasons for doing things that way.

    > Here is an example of what one might want to write:
    >
    > <start file1.h>
    > inline int file1_f( int k );
    >
    > <end file1.h>
    >
    > <start file1.c>
    > static inline int g( int k )
    > {
    > return k+1;
    > }
    >
    > inline int file1_f( int k )
    > {
    > return 2*k + g(k);
    > }
    >
    > <end file1.c>


    Is there any reason why you couldn't correct the problem with this
    code, by either adding 'static', or removing 'inline', from the
    declaration of file1_f()?
     
    James Kuyper, Nov 20, 2003
    #2
    1. Advertising

  3. Daniel Vallstrom

    Kevin Bracey Guest

    In message <>
    (Daniel Vallstrom) wrote:

    > Here is an example of what one might want to write:
    >
    > <start file1.h>
    > inline int file1_f( int k );
    >
    > <end file1.h>
    >
    > <start file1.c>
    > static inline int g( int k )
    > {
    > return k+1;
    > }
    >
    > inline int file1_f( int k )
    > {
    > return 2*k + g(k);
    > }
    >
    > <end file1.c>
    >
    >
    > Why isn't this allowed? If the g call in file1_f is inlined
    > there is no problem? And if the g call isn't inlined why
    > can't the compiler choose to not inline file1_f calls, or
    > for that matter choose to inline file1_f calls and handle
    > the g call in some way? What's the problem?


    It's well weird coding practice, what you've done there. And that's
    kind of why it's disallowed. You wouldn't put the definition of
    file_f in file1.c - you would normally put it in file1.h, so every function
    including file1.h could have an inline definition.

    Indeed, anyone just including file1.h as it stands would get a diagnostic
    if they didn't provide their own inline definition...

    So, if the inline definition (which would normally be in the header file)
    used a static function, then that would normally indicate a programming
    error, as it would imply that you weren't actually providing the same
    functionality as the inline definition in another translation unit.

    Now, let's say that file1_f is actually providing the external definition
    of file_f, so you write:

    extern inline int file1_f( int k )
    {
    return 2*k + g(k);
    }

    Now that would be legal. The external definition of file_f is allowed to
    be different, and is allowed to reference static functions.

    The diagnostic in question is a real pain to produce, as it doesn't
    follow naturally from the code generation. Especially as you have to
    defer generating it until you've read the whole file and know for certain
    that you have an inline definition and not an external one.

    --
    Kevin Bracey, Principal Software Engineer
    Tematic Ltd Tel: +44 (0) 1223 503464
    182-190 Newmarket Road Fax: +44 (0) 1223 503458
    Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
     
    Kevin Bracey, Nov 21, 2003
    #3
    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. Don Golding
    Replies:
    0
    Views:
    657
    Don Golding
    Apr 23, 2004
  2. =?Utf-8?B?UWluZ2RvbmcgWi4=?=

    Internal or External Reqest

    =?Utf-8?B?UWluZ2RvbmcgWi4=?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    329
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Oct 26, 2004
  3. Replies:
    3
    Views:
    473
  4. Rahul
    Replies:
    3
    Views:
    463
    James Kanze
    Feb 28, 2008
  5. Wendy Elizabeth

    internal web service calling multiply types of external web servic

    Wendy Elizabeth, Nov 6, 2004, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    163
    Dan Rogers
    Nov 15, 2004
Loading...

Share This Page