Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 934
Thomas Matthews Avatar asked Feb 16 '12 19:02

Thomas Matthews


People also ask

What is forward declaration of a function?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

How do you forward a declaration?

To write a forward declaration for a function, we use a function declaration statement (also called a function prototype). The function declaration consists of the function header (the function's return type, name, and parameter types), terminated with a semicolon. The function body is not included in the declaration.

How do you declare an inline function?

Inline function and classes: If you need to explicitly declare inline function in the class then just declare the function inside the class and define it outside the class using inline keyword.

What is forward declaration C?

As others stated before, a forward declaration in C/C++ is the declaration of something with the actual definition unavailable. Its a declaration telling the compiler "there is a data type ABC".


2 Answers

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>.

like image 159
ildjarn Avatar answered Oct 12 '22 23:10

ildjarn


You don't need to "forward declare" it (a term that is usually only applied to types, since we usually define them in the same place that we first declare them).

Just declare it, like normal:

#include <iostream>

void foo();            // Declaration

inline void foo() {    // Defining declaration
   std::cout << "!";
}

// ---------------------------------------------

int main() {
   foo();              // Output: foo()
}

Live demo.

like image 26
Lightness Races in Orbit Avatar answered Oct 12 '22 22:10

Lightness Races in Orbit