linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* proper declaration/definition of inline functions?
@ 2005-08-02 19:02 Robert P. J. Day
  2005-08-03  6:08 ` Steve Graegert
  2005-08-03  8:51 ` Glynn Clements
  0 siblings, 2 replies; 3+ messages in thread
From: Robert P. J. Day @ 2005-08-02 19:02 UTC (permalink / raw)
  To: C programming list


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

rday

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: proper declaration/definition of inline functions?
  2005-08-02 19:02 proper declaration/definition of inline functions? Robert P. J. Day
@ 2005-08-03  6:08 ` Steve Graegert
  2005-08-03  8:51 ` Glynn Clements
  1 sibling, 0 replies; 3+ messages in thread
From: Steve Graegert @ 2005-08-03  6:08 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: C programming list

On 8/2/05, Robert P. J. Day <rpjday@mindspring.com> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: proper declaration/definition of inline functions?
  2005-08-02 19:02 proper declaration/definition of inline functions? Robert P. J. Day
  2005-08-03  6:08 ` Steve Graegert
@ 2005-08-03  8:51 ` Glynn Clements
  1 sibling, 0 replies; 3+ messages in thread
From: Glynn Clements @ 2005-08-03  8:51 UTC (permalink / raw)
  To: Robert P. J. Day; +Cc: C programming list


Robert P. J. Day 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.)

You have to define them (as "static inline") in the header file.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-08-03  8:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-02 19:02 proper declaration/definition of inline functions? Robert P. J. Day
2005-08-03  6:08 ` Steve Graegert
2005-08-03  8:51 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).