about extern inline and static inline

Discussion in 'C++' started by Sean, Apr 29, 2006.

  1. Sean

    Sean Guest

    I am a little confused by the "extern inline and static inline" rules. I
    understand that "extern inline" guarantees no function storage is
    created (all are inlined). But the following test seems to contradict
    what I expect---I found inc_i() is actually compiled as a linkable
    function in the a.out
    >g++ main.cpp call_inc.cpp
    >nm a.out

    ......
    0804875e W _Z5inc_iv

    Can someone explain what's wrong with my understanding? Even better, I
    would appreciate if you can show me the comprehensive rule of "static
    inline" and "extern inline".

    Thanks,
    Sean

    --------------------------------
    //myheader.h
    extern int i;
    extern void call_inc_i();
    extern inline void inc_i(){
    i++;
    }

    -------------------------------
    //main.cpp
    #include <iostream>
    #include "myheader.h"
    using namespace std;
    int i = 0;
    int main(){
    inc_i();
    cout << "after inc_i()" << i << endl;
    call_inc_i();
    cout << "after call_inc_i()" << i << endl;
    }

    --------------------------------
    //call_inc.cpp
    #include "myheader.h"
    void call_inc_i(){
    inc_i();
    }
    --------------------------------------
     
    Sean, Apr 29, 2006
    #1
    1. Advertisements

  2. Sean wrote:
    > I am a little confused by the "extern inline and static inline" rules. I
    > understand that "extern inline" guarantees no function storage is
    > created (all are inlined).


    This is incorrect in C++ (and I suspect it is an extension of gcc).
    Inlining never guarantees anything (expect that the compiler will *try*
    to inline it).

    > But the following test seems to contradict
    > what I expect---I found inc_i() is actually compiled as a linkable
    > function in the a.out


    This is a normal behavior. Not all functions may be inlined. Compilers
    are usually better than we are at inlining functions.

    > Can someone explain what's wrong with my understanding? Even better, I
    > would appreciate if you can show me the comprehensive rule of "static
    > inline" and "extern inline".


    inline
    tells the compiler to try to inline that function (it may refuse for
    various reasons)

    extern
    redundant for a function but for an object, transforms a definition
    into a declaration

    static
    for a namespace-scope name (not in a class), makes it local to the
    translation
    unit (roughly the file in which it is defined). However, this is
    deprecated in C++,
    unnamed namespaces should be used instead.

    static inline and static extern are both combinations of the two terms.

    Note that this does not apply to extern "C", which is a different
    beast.


    Jonathan
     
    Jonathan Mcdougall, Apr 29, 2006
    #2
    1. Advertisements

  3. Sean

    Sean Guest

    Jonathan Mcdougall wrote:
    > Sean wrote:
    >
    >>I am a little confused by the "extern inline and static inline" rules. I
    >>understand that "extern inline" guarantees no function storage is
    >>created (all are inlined).

    >
    >
    > This is incorrect in C++ (and I suspect it is an extension of gcc).
    > Inlining never guarantees anything (expect that the compiler will *try*
    > to inline it).
    >
    >
    >>But the following test seems to contradict
    >>what I expect---I found inc_i() is actually compiled as a linkable
    >>function in the a.out

    >
    >
    > This is a normal behavior. Not all functions may be inlined. Compilers
    > are usually better than we are at inlining functions.
    >
    >
    >>Can someone explain what's wrong with my understanding? Even better, I
    >>would appreciate if you can show me the comprehensive rule of "static
    >>inline" and "extern inline".

    >
    >
    > inline
    > tells the compiler to try to inline that function (it may refuse for
    > various reasons)
    >
    > extern
    > redundant for a function but for an object, transforms a definition
    > into a declaration
    >
    > static
    > for a namespace-scope name (not in a class), makes it local to the
    > translation
    > unit (roughly the file in which it is defined). However, this is
    > deprecated in C++,
    > unnamed namespaces should be used instead.
    >
    > static inline and static extern are both combinations of the two terms.

    but if extern inline means "extern + inline" (simple combination), there
    exist two definition of incr_i() in my example--since myheader.h is
    included in main.cpp and call_inc.cpp.
    There must be some special meaning besides simple combination, I guess.

    >
    > Note that this does not apply to extern "C", which is a different
    > beast.
    >
    >
    > Jonathan
    >
     
    Sean, Apr 29, 2006
    #3
  4. Sean wrote:
    > Jonathan Mcdougall wrote:
    > > Sean wrote:
    > >
    > > inline
    > > tells the compiler to try to inline that function (it may refuse for
    > > various reasons)
    > >
    > > extern
    > > redundant for a function but for an object, transforms a definition
    > > into a declaration
    > >
    > > static
    > > for a namespace-scope name (not in a class), makes it local to the
    > > translation
    > > unit (roughly the file in which it is defined). However, this is
    > > deprecated in C++,
    > > unnamed namespaces should be used instead.


    I have to change that last sentence. Only using static on objects in a
    namespace scope is deprecated. Using static on functions is legal.

    > > static inline and static extern are both combinations of the two terms.


    > but if extern inline means "extern + inline" (simple combination), there
    > exist two definition of incr_i() in my example--since myheader.h is
    > included in main.cpp and call_inc.cpp.
    > There must be some special meaning besides simple combination, I guess.


    No. As I said, extern for a function is redundant (a function always
    has external linkage, unless it is static) so let's forget about it. If
    a function is inline, its *definition* (body) is actually *required* in
    each translation unit it used. Think: if the definition is not
    available, how can it be inlined? Multiple definitions (in different
    translation units) of an inline function is not only legal, but
    required.

    If the function is both static and inline, it may confuse the compiler,
    which will probably decide not to inline the function. Because the
    function is static, each translation unit will get its copy. If the
    compiler honors the inline hint, each translation unit will have its
    copy of the function inlined.


    Jonathan
     
    Jonathan Mcdougall, Apr 29, 2006
    #4
  5. Sean

    Rolf Magnus Guest

    Sean wrote:

    > I am a little confused by the "extern inline and static inline" rules. I
    > understand that "extern inline" guarantees no function storage is
    > created (all are inlined).


    No, it doesn't guarantee that at all. On the contrary. If a function is
    extern, it must be linkable, so a non-inlined version must usually exist.
    Using 'inline' doesn't really guarantee inlining at all. The only effect of
    it regarding the C++ standard is that you can violate the
    one-definition-rule. To the compiler, it's just a mere hint that you would
    like the function to be inlined. The compiler might choose to ignore it, or
    it might choose to inline functions that are not declared 'inline'.

    > But the following test seems to contradict what I expect---I found inc_i()
    > is actually compiled as a linkable
    > function in the a.out
    > >g++ main.cpp call_inc.cpp
    > >nm a.out

    > .....
    > 0804875e W _Z5inc_iv
    >
    > Can someone explain what's wrong with my understanding? Even better, I
    > would appreciate if you can show me the comprehensive rule of "static
    > inline" and "extern inline".


    static means that the function can only be used in the translation unit
    where it's defined. extern means it can be used in any translation unit of
    the program. inline means that a function can be defined in multiple
    translation units without an error.
     
    Rolf Magnus, Apr 30, 2006
    #5
    1. Advertisements

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. A
    Replies:
    5
    Views:
    3,745
    Victor Bazarov
    Nov 27, 2003
  2. Thomas Matthews
    Replies:
    5
    Views:
    3,191
    tom_usenet
    Aug 2, 2004
  3. ik

    extern and static

    ik, Sep 22, 2004, in forum: C++
    Replies:
    2
    Views:
    601
    Rob Williscroft
    Sep 22, 2004
  4. Dominik Rau
    Replies:
    3
    Views:
    940
    Dominik Rau
    Apr 25, 2005
  5. Magix

    extern and Static

    Magix, Oct 6, 2004, in forum: C Programming
    Replies:
    5
    Views:
    842
    Ben Pfaff
    Oct 9, 2004
  6. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    916
    Army1987
    Oct 8, 2007
  7. regis
    Replies:
    3
    Views:
    2,085
    regis
    Oct 10, 2008
  8. Andre
    Replies:
    5
    Views:
    819
    Keith Thompson
    Jul 17, 2012
Loading...