Any preprocessor definition to tell if inline is supported?

Discussion in 'C Programming' started by Martin Wells, Sep 24, 2007.

  1. Martin Wells

    Martin Wells Guest

    Is there anything like __inline_is_supported to indicate whether a
    certain C compiler supports inline?

    I'm writing my code as fully portable C89, but I want to use inline.

    Martin
     
    Martin Wells, Sep 24, 2007
    #1
    1. Advertising

  2. On Mon, 24 Sep 2007 12:18:10 -0700, Martin Wells wrote:
    > Is there anything like __inline_is_supported to indicate whether a
    > certain C compiler supports inline?
    >
    > I'm writing my code as fully portable C89, but I want to use inline.


    No C89 compiler supports inline. They're not allowed to, since C89 allows
    programmers to name their own objects and functions "inline". In other
    words, this program is valid C89:

    #include <stdio.h>
    int main(int inline, char *argv[]) {
    if (inline >= 2)
    puts(argv[1]);
    return 0;
    }

    Some compilers support something that looks like C89, but adds inline.
    These are not C89 compilers. They refuse to compile valid programs such
    as the above.

    You can, however, remain portable by writing in the common subset of C89
    and C99, and then you can use

    #if __STDC_VERSION__ < 199901L
    #define inline /* nothing */
    #endif

    static inline int zero(void) {
    return 0;
    }
     
    =?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=, Sep 24, 2007
    #2
    1. Advertising

  3. Martin Wells <> writes:

    > Is there anything like __inline_is_supported to indicate whether a
    > certain C compiler supports inline?
    >
    > I'm writing my code as fully portable C89, but I want to use inline.


    I use the following myself:

    #ifndef __GNUC__
    # if __STDC_VERSION__ + 0 >= 199901L
    # define __inline__ inline
    # define __restrict__ restrict
    # else
    # define __inline__
    # define __restrict__
    # endif
    #endif

    __inline__ and __restrict__ identifiers are reserved though so it may
    have some portability issues. __inline__ and __restrict__ keywords come
    from GCC. It allows using them even if you are compiling your program
    in C89 mode where there is no inline keyword.

    --
    Best regards, _ _
    .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
    ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
    ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
     
    Michal Nazarewicz, Sep 24, 2007
    #3
  4. Martin Wells

    Martin Wells Guest

    Harald:

    > No C89 compiler supports inline. They're not allowed to, since C89 allows
    > programmers to name their own objects and functions "inline". In other
    > words, this program is valid C89:
    >
    > #include <stdio.h>
    > int main(int inline, char *argv[]) {
    > if (inline >= 2)
    > puts(argv[1]);
    > return 0;
    >
    > }
    >
    > Some compilers support something that looks like C89, but adds inline.
    > These are not C89 compilers. They refuse to compile valid programs such
    > as the above.



    While these "C89 + extras compilers" may fail to compile the code you
    show above, it's not inconceivable that a compliant C compiler could
    work with the following:

    inline int Func(void) { return 7; }

    int main(void)
    {
    int inline = 6;

    return 0;
    }

    There's already plenty of different C keywords that have different
    meanings in different contexts. (Granted none of them can be used as
    identifiers, but you get the picture).


    > You can, however, remain portable by writing in the common subset of C89
    > and C99, and then you can use
    >
    > #if __STDC_VERSION__ < 199901L
    > #define inline /* nothing */
    > #endif
    >
    > static inline int zero(void) {
    > return 0;



    I'd be hesitant to use static, given that inline functions are, by
    default, extern. Off the top of my head I can't think of any drawbacks
    of making them static, but I know from past experience not to jump
    into something just because I can't see the drawbacks off the top of
    my head.

    Can anyone see any drawbacks of making an inline function static?
    (Except for being able to declare but not define it in another
    translation unit).

    And by the way, I do realise I could use further macro trickery to get
    rid of the static.

    Martin
     
    Martin Wells, Sep 24, 2007
    #4
  5. On Mon, 24 Sep 2007 22:30:25 +0200, Michal Nazarewicz wrote:
    > Martin Wells <> writes:
    >
    >> Is there anything like __inline_is_supported to indicate whether a
    >> certain C compiler supports inline?
    >>
    >> I'm writing my code as fully portable C89, but I want to use inline.

    >
    > I use the following myself:
    >
    > #ifndef __GNUC__
    > # if __STDC_VERSION__ + 0 >= 199901L
    > # define __inline__ inline
    > # define __restrict__ restrict
    > # else
    > # define __inline__
    > # define __restrict__
    > # endif
    > #endif
    >
    > __inline__ and __restrict__ identifiers are reserved though so it may
    > have some portability issues. __inline__ and __restrict__ keywords come
    > from GCC. It allows using them even if you are compiling your program
    > in C89 mode where there is no inline keyword.


    You can go the other way around:

    #if __STDC_VERSION__ >= 199901L
    /*
    * Uncomment this if you like:
    #define inline inline
    #define restrict restrict
    */
    #elif defined(__GNUC__)
    #define inline __inline__
    #define restrict __restrict__
    #elif defined(__Special_Other_Compiler__)
    #define inline __inline
    #define restrict /* nothing */
    #else
    #define inline /* nothing */
    #define restrict /* nothing */
    #endif

    This, strictly speaking, still isn't valid C89, but it will work using
    implementation extensions on GCC and SOC, and other C89 implementations
    that don't define __GNUC__ or __Special_Other_Compiler__ must accept it
    since you don't define any reserved identifiers this way. Additionally,
    it is valid C99.

    The reason it still isn't valid C89, is because an implementation is
    allowed to define __GNUC__ without supporting any GNU extensions, and
    similarly for SOC. Judging from what you're currently using, you don't
    care about supporting such an implementation. I wouldn't either.

    By the way, you don't need __STDC_VERSION__ + 0. Macros that are not
    defined expand to 0 in #if expressions.
     
    =?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F, Sep 24, 2007
    #5
  6. In article <>,
    user923005 <> wrote:

    >For compilers that offer inline, many allow you to set a compiler
    >option to "inline any suitable function" which will not require any
    >decoration of the function name and will also probably guess better
    >than you will what to inline.


    On the other hand, declaring your functions inline may cause them to
    be inlined on compilers that other people use to compile your program,
    and whose options you don't know.

    -- Richard



    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 24, 2007
    #6
  7. Martin Wells

    user923005 Guest

    On Sep 24, 12:18 pm, Martin Wells <> wrote:
    > Is there anything like __inline_is_supported to indicate whether a
    > certain C compiler supports inline?
    >
    > I'm writing my code as fully portable C89, but I want to use inline.


    Inline is just a compiler hint. The compiler is free to ignore you.

    For compilers that offer inline, many allow you to set a compiler
    option to "inline any suitable function" which will not require any
    decoration of the function name and will also probably guess better
    than you will what to inline.
     
    user923005, Sep 24, 2007
    #7
  8. On Mon, 24 Sep 2007 13:43:43 -0700, Martin Wells wrote:
    > Harald:
    >
    >> No C89 compiler supports inline. They're not allowed to, since C89
    >> allows programmers to name their own objects and functions "inline". In
    >> other words, this program is valid C89:
    >>
    >> #include <stdio.h>
    >> int main(int inline, char *argv[]) {
    >> if (inline >= 2)
    >> puts(argv[1]);
    >> return 0;
    >>
    >> }
    >>
    >> Some compilers support something that looks like C89, but adds inline.
    >> These are not C89 compilers. They refuse to compile valid programs such
    >> as the above.

    >
    >
    > While these "C89 + extras compilers" may fail to compile the code you
    > show above, it's not inconceivable that a compliant C compiler could
    > work with the following:
    >
    > inline int Func(void) { return 7; }
    >
    > int main(void)
    > {
    > int inline = 6;
    >
    > return 0;
    > }


    I'm not aware of any implementations that do this, but it's a fair point.
    I don't believe there's any code that would break this way (although for
    the other new keyword, restrict, there is).

    > There's already plenty of different C keywords that have different
    > meanings in different contexts. (Granted none of them can be used as
    > identifiers, but you get the picture).
    >
    >> You can, however, remain portable by writing in the common subset of
    >> C89 and C99, and then you can use
    >>
    >> #if __STDC_VERSION__ < 199901L
    >> #define inline /* nothing */
    >> #endif
    >>
    >> static inline int zero(void) {
    >> return 0;

    >
    >
    > I'd be hesitant to use static, given that inline functions are, by
    > default, extern.


    Non-inline functions are always extern or static, but inline functions
    are by default neither.

    > Off the top of my head I can't think of any drawbacks
    > of making them static, but I know from past experience not to jump into
    > something just because I can't see the drawbacks off the top of my head.


    static inline int zero(void) { return 0; }
    int foo(void) { return zero(); }

    This calls the local zero function. It can be defined in as many units as
    you like; since it's static, they won't conflict.

    extern inline int zero(void) { return 0; }
    int foo(void) { return zero(); }

    This calls the local zero function. It can only be defined in this one
    unit. If any other unit defines zero this way as well, it is an error.

    inline int zero(void) { return 0; }
    int foo(void) { return zero(); }

    This calls either the local zero function, or some zero function defined
    in another unit, depending on whether the compiler feels like inlining.
    If no zero function is defined in another unit, it is an error, which may
    go undetected.

    (Note that this is what C99 gives you; it differs from certain C89
    extensions to specify inlining.)
     
    =?iso-2022-kr?q?Harald_van_D=0E=29=26=0Fk?=, Sep 24, 2007
    #8
  9. Martin Wells

    Eric Sosman Guest

    user923005 wrote On 09/24/07 17:27,:
    > On Sep 24, 12:18 pm, Martin Wells <> wrote:
    >
    >>Is there anything like __inline_is_supported to indicate whether a
    >>certain C compiler supports inline?
    >>
    >>I'm writing my code as fully portable C89, but I want to use inline.

    >
    >
    > Inline is just a compiler hint. The compiler is free to ignore you.
    >
    > For compilers that offer inline, many allow you to set a compiler
    > option to "inline any suitable function" which will not require any
    > decoration of the function name and will also probably guess better
    > than you will what to inline.


    "What he said." Also, be aware that the semantics
    of `inline' in a C89-with-extras compiler may not be
    exactly those used by a C99 compiler. Just knowing that
    Frobozz Magic C can be made to recognize `inline' as a
    keyword doesn't mean you can be sure what it means.

    --
     
    Eric Sosman, Sep 24, 2007
    #9
  10. Martin Wells

    Richard Bos Guest

    Martin Wells <> wrote:

    > Harald:
    >
    > > No C89 compiler supports inline. They're not allowed to, since C89 allows
    > > programmers to name their own objects and functions "inline". In other
    > > words, this program is valid C89:
    > >
    > > #include <stdio.h>
    > > int main(int inline, char *argv[]) {
    > > if (inline >= 2)
    > > puts(argv[1]);
    > > return 0;
    > >
    > > }


    > While these "C89 + extras compilers" may fail to compile the code you
    > show above, it's not inconceivable that a compliant C compiler could
    > work with the following:
    >
    > inline int Func(void) { return 7; }
    >
    > int main(void)
    > {
    > int inline = 6;
    >
    > return 0;
    > }


    Possibly, but that would still leave cases like

    /* Input is on the 4th line of the card */
    #define inline 4

    inline int func(void); /* Oops... */

    Richard
     
    Richard Bos, Sep 25, 2007
    #10
    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. RC
    Replies:
    1
    Views:
    1,047
    Martin Honnen
    Sep 27, 2005
  2. Cronus
    Replies:
    1
    Views:
    715
    Paul Mensonides
    Jul 15, 2004
  3. yogesh
    Replies:
    1
    Views:
    398
    Victor Bazarov
    Mar 14, 2007
  4. Yugui (Yuki Sonoda)
    Replies:
    4
    Views:
    243
    Matthias Tarasiewicz
    Feb 1, 2009
  5. Mike A
    Replies:
    17
    Views:
    286
    Dr John Stockton
    Nov 19, 2003
Loading...

Share This Page