Search code examples
c++cinlinec99forward-declaration

Forward declaration of inline functions


I have a header file that is going to contain a large amount (30+) of inline functions.

Rather than having the reader scroll or search for the definition (implementation) of the inline function, I would like to have a forward declaration section that states the function declaration with comments describing the function. This section would allow the reader to find out how to use a function or to look for a function without having to scroll down to the implementation.

Also, I would like the readers to get in the habit of using functions without having to see their implementations.

What is the syntax for a forward declaration of a stand-alone function?

{This applies to C99 and C++}

FYI, I am using IAR Workbench C compiler set to use C99.


Solution

  • No differently than a non-inline function:

    void func();       // "forward" declaration
    
    // ...
    
    inline void func() // definition
    {
        // impl
    }
    

    Typically the pattern used to "hide" the definitions from the library consumer is to put the declarations in one header (a.h) and the definitions in a second header (a_def.h), then have the former #include the latter (inclusion guards omitted for brevity):

    // a.h
    void func();
    #include "a_def.h"
    
    // a_def.h
    inline void func()
    {
        // impl
    }
    

    The library consumer would simply #include <a.h>.