linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steve Graegert <graegerts@gmail.com>
To: "Robert P. J. Day" <rpjday@mindspring.com>
Cc: C programming list <linux-c-programming@vger.kernel.org>
Subject: Re: proper declaration/definition of inline functions?
Date: Wed, 3 Aug 2005 08:08:51 +0200	[thread overview]
Message-ID: <6a00c8d505080223085b689518@mail.gmail.com> (raw)
In-Reply-To: <Pine.LNX.4.63.0508021501100.29269@localhost.localdomain>

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

  reply	other threads:[~2005-08-03  6:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-02 19:02 proper declaration/definition of inline functions? Robert P. J. Day
2005-08-03  6:08 ` Steve Graegert [this message]
2005-08-03  8:51 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d505080223085b689518@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=rpjday@mindspring.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).