On 8/2/05, Robert P. J. Day <[EMAIL PROTECTED]> wrote:
>
> what is the proper usage of inline functions? would you first
> declare it inline in a header file, and also define it inline in the
> corresponding source file? (yes, i know the compiler is under no
> obligation to inline them ... i just want to know the canonical way to
> use them.)
Robert,
Inline functions always have internal linkage, that is they cannot be
seen outside of their translation unit. So you cannot prototype them
in a header file and put the code in a .cpp file someplace. Put the
code in the header file, so it will be included into every translation
unit where it is needed.
In C++ a general approach to using inline functions is to declare and
define them as usual:
class C {
public:
inline void func();
};
inline void C::func() {
// Do some work here
}
Please not that member functions don't need to be inlined explicitly.
The code above is practically the same as:
class C {
public:
void func() {
// Do some work here
}
};
A C++ compiler usually tries to inline member functions automatically
and decides on its own whether inlining is feasible or not.
When inlining C functions there are two major techniques known to me.
One way is to adhere to the C99 recommendation and use the inline
keyword in conjunction with extern:
/* myheader.h */
inline int add(int a, int b) {
return (a + b);
}
/* translation unit */
#include "myheader.h"
extern int add(int a, int b);
The other approach is known as the "GNU C inlining model" that simply
defines the inline function in a common header via extern and includes
it in only _one_ source file
/* myheader.h */
#ifndef INLINE_DECL
# define INLINE_DECL extern inline
#endif
INLINE_DECL int add(int a, int b) {
return (a + b);
}
/* translation unit */
#define INLINE_DECL
#include "myheader.h"
Finally, you can easily inline functions as static in a common header file
static inline int add(int a, int b) {
return (a + b);
}
but to support legacy compilers you will have to provide the
preprocessor directive -DINLINE="" to remove the inline keyword wich
is not part of ANSI C.
Regards
\Steve
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming"
in
the body of a message to [EMAIL PROTECTED]
More majordomo info at http://vger.kernel.org/majordomo-info.html