inline functions

Discussion in 'C++' started by Mani, Dec 18, 2010.

  1. Mani

    Mani Guest

    Experts, I would like to know how inline, 'static inline' and 'extern
    inline' functions differs. Which one should be used when.
    Mani, Dec 18, 2010
    #1
    1. Advertising

  2. Mani

    Armel Guest

    "Mani" <> a écrit dans le message de news:
    ...
    > Experts, I would like to know how inline, 'static inline' and 'extern
    > inline' functions differs. Which one should be used when.


    static and extern are totally independent from "inline".

    static tells that the function is local to the module (for global
    functions), or class functions in opposition to object functions (for
    functions declared inside a struct/union/class definition)

    extern is typically not used with functions prototypes but i believe some
    compilers use that to declare the function being part of the external
    interface of the built program (e.g. public function of .DLL or .so)

    --
    Armel
    Armel, Dec 18, 2010
    #2
    1. Advertising

  3. Mani

    Öö Tiib Guest

    On Dec 18, 6:41 pm, Mani <> wrote:
    > Experts, I would like to know how  inline, 'static inline' and 'extern
    > inline' functions differs. Which one should be used when.


    There are no such things in C++ as 'static inline' and 'extern
    inline'. These are C terms described in C99. So if you want to know
    the details read that standard or ask in C group.

    In C++ 'inline' keyword is an hint to compiler that the function may
    be inlined. Compiler has still quite free hands to inline or not to
    inline what it pleases as long it does not cause externally visible
    side effects.

    Biggest effects of 'inline' keyword in C++ are that the definition of
    function must be in code before the code calling it and that inline
    function may be defined in the header file without getting linker
    errors about that function being defined in several translation units
    that did include the header file.
    Öö Tiib, Dec 19, 2010
    #3
  4. Öö Tiib <> wrote:
    > In C++ 'inline' keyword is an hint to compiler that the function may
    > be inlined. Compiler has still quite free hands to inline or not to
    > inline what it pleases as long it does not cause externally visible
    > side effects.


    Actually, does any modern C++ compiler take the 'inline' keyword
    *at all* into consideration when deciding whether to inline a function
    or not? I have the impression that, from the optimization point of view,
    the 'inline' keyword is a no-op (in the exact same way as eg. the
    'register' keyword is). Hence the 'inline' keyword has basically lost
    its meaning as an optimization hint keyword and become purely an
    instruction for the linker.

    > Biggest effects of 'inline' keyword in C++ are that the definition of
    > function must be in code before the code calling it and that inline
    > function may be defined in the header file without getting linker
    > errors about that function being defined in several translation units
    > that did include the header file.


    Also note that some functions are implicitly 'inline' even without
    having to explicitly specify that keyword. More specifically, member
    functions which are defined in the class definition, as well as templated
    functions (ok, there might be some technical difference between a templated
    function and an inline function, but the end result is basically the same).

    Eg:

    class SomeClass
    {
    public:
    // This function is 'inline' without having to specify the
    // keyword explicitly:
    void foo() { std::cout << "hello\n"; }

    void bar();
    };

    // This function is *not* 'inline' if you don't specify the keyword
    // explicitly here (hence you can get a linker error if this is
    // included in more than one compilation unit):
    void SomeClass::bar() { "there\n"; }
    Juha Nieminen, Dec 19, 2010
    #4
  5. Mani

    Virchanza Guest

    On Dec 18, 4:41 pm, Mani <> wrote:
    > Experts, I would like to know how  inline, 'static inline' and 'extern
    > inline' functions differs. Which one should be used when.



    Forgetting about template functions for a moment (which work a little
    differently):

    Every function, whether it be inline or not, is either "extern" or
    "static".

    "extern" means that the function is available outside of the
    translation unit in which it is defined.

    "static" means that the function is only available inside the
    translation unit in which it is defined.

    By default, all functions are "extern" unless you explicitly write the
    word "static" in front of them.

    The following function:

    inline int Func() { return 5; }

    is the same as:

    extern inline int Func() { return 5; }

    You can however define an inline function as static:

    static inline int Func() { return 5; }

    The "static" keyword won't have any noticeable effect upon an inline
    function unless the function contains a static object, e.g.:

    static inline int Func()
    {
    static int i = 0;

    return i++;
    }

    Because Func is static, there will be a separate "i" object for every
    translation unit that defines this function. Normally, if Func were
    extern, there would only be one "i" object for the entire program.
    Virchanza, Dec 19, 2010
    #5
  6. Mani

    SG Guest

    On 18 Dez., 17:41, Mani <> wrote:
    > Experts, I would like to know how  inline, 'static inline' and 'extern
    > inline' functions differs. Which one should be used when.


    They differ in linkage. The "inline" keyword has little to do with
    this. These two concepts are orthogonal.

    Linkage determines whether you can refer to an entity directly by name
    from another translation unit (external linkage) or not (internal
    linkage). Some prefer to use other terms like "global scope" and "file
    scope" to differentiate between external and internal linkage. Note
    also that the keyword "static" has three different uses. Its meaning
    changes depending on the context. One of them is to give entities
    internal linkage. As such, it's kind of the opposite of "extern" in
    that case. Many things have external linkage by default in which case
    the use of "extern" would be redundant.

    But in C (not C++) "inline" usually goes with "static" because of the
    way inline works in C. In C++ you can simply put a definition of an
    inline function into a header file and include it into multiple
    translation units without having the linker complain to you about
    multiple definitions. The ODR (one definition rule) makes an exception
    for inline functions and template functions. What you have to keep in
    mind, though, is that if you use an inline function in a translation
    unit, this translation unit also has to contain a definition for it. I
    would go as far as saying that this is actually the point of "inline".
    Actual inlining for a compiler is made easy if it has easy access to
    the function's definition. The inline keyword is a way of making sure
    the definition is available in the same translation unit.
    SG, Dec 19, 2010
    #6
  7. Virchanza <> wrote:
    > static inline int Func() { return 5; }
    >
    > The "static" keyword won't have any noticeable effect upon an inline
    > function unless the function contains a static object, e.g.:


    Well, strictly speaking the value of the function pointer will
    depend on the compilation unit as well. (Not that making this
    distinction is anything common.)
    Juha Nieminen, Dec 20, 2010
    #7
    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. Abhi
    Replies:
    2
    Views:
    734
    E. Robert Tisdale
    Jul 3, 2003
  2. Replies:
    3
    Views:
    457
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    1,875
    Kevin Bracey
    Nov 21, 2003
  4. jamihuq

    converting inline functions to C functions

    jamihuq, May 16, 2006, in forum: C Programming
    Replies:
    7
    Views:
    344
  5. Rahul
    Replies:
    3
    Views:
    454
    James Kanze
    Feb 28, 2008
Loading...

Share This Page