Where to put the inline keyword

Pages: 12
With inline member functions that are defined outside the class,is it best to put the inline keyword next to the declaration within the class body,next to the defintion outside the class body,or both.

example :
//inline keyword next to definition
class base
{
public :void fun();
};
inline void base ::fun()
{
}


//inline keyword next to the declaration
class base
{
public :inline void fun();
};
void base ::fun()
{
}

//inline keyword next to the declaration and definition
class base
{
public :inline void fun();
};
inline void base ::fun()
{
}


I think this is an interesting question. These are my thoughts:

First of all, I don't think it's good practice to put function declarations on the same line as "public:". I don't know if you did this only in this post, or if you actually do this in your code.

The second alternative (keyword next to the declaration) surprises me, because I didn't think that was possible. I thought the keyword next to the definition was demanded, and the keyword next to the declaration was optional and redundant.

IMHO the first alternative (keyword next to definition) is the best, because whether the function is inline or not is independant from the class interface. In this case functions can be inlined/uninlined without changing how the class is defined.
closed account (z05DSL3A)
Inline member functions need to be in the definition/a header file, so that when the compiler compiles a client the client can include the inline function definition in place.

So should be
1
2
3
4
5
6
7
8
class base
{
    void noinline();
    inline void fun(){...}
};

Void Base:: noinline()
{...}
Last edited on
Void Base:: noinline()
should be
void base::noinline()
right?
@Grey Wolf:
The OP wrote: "With inline member functions that are defined outside the class [...]". In your code, you defined the function fun inside the class.
The C++ FAQ Lite has an excellent bit regarding this.

http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.7
Last edited on
closed account (z05DSL3A)
@Ropex,
Yes I defined the function fun inside the class. I also said ‘Inline member functions need to be in the definition’ ie you do not define an inline function outside the class definition.

@rpgfan3233
Re my typo: I’m dyslexic so I tend to write my posted in word and cut and paste them, this is a case of word correcting thing and me not checking before posting. Sorry.
@Grey Wolf:
>> you do not define an inline function outside the class definition

Of course you do! This is perfectly possible:
1
2
3
4
5
6
// foo.h
class Foo {
public:
    void foo();
};
inline void Foo::foo() {}
@Grey Wolf:
>> I’m dyslexic so I tend to write my posted in word and cut and paste them, this is a case of word correcting thing and me not checking before posting. Sorry.

I use opera, and before I commit, I can right click the edit box, and select Check spelling.
@Grey Wolf:
> Re my typo: I’m dyslexic so I tend to write my posted in word and cut and paste them, this is a case of word correcting thing and me not checking before posting. Sorry.

No problem. I'm just trying to make sure we're on track, so we don't confuse people that need help. ^_^

Edit: As for defining an inline function outside of the class, it is perfectly valid. 'inline' refers to expanding the function definition with the object, not the idea that the function is defined inside or outside the class.

To quote the C++ FAQ Lite:
> It's usually imperative that the function's definition (the part between the {...}) be placed in a header file.
> If you put the inline function's definition into a .cpp file,
> and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker.
Last edited on
@nezi:

I did some research with GCC, and came up with a couple of things. Sorry if you already know this, but I write it partly to correct my previous post.

First of all, all three alternatives work (at least with GCC).

Second, this is an error:
1
2
3
4
5
6
7
8
9
// foo.h
class Foo {
public:
    inline void foo();
};

// foo.cpp
#include "foo.h"
void Foo::foo() {}

This doesn't compile if foo.h is included in another cpp file.

For this reason, I think it's best not to include the inline keyword in the declaration. If it is later decided to uninline the function, it can simply be removed from the header file, and it only needs to be changed in one place. Sometimes I put inline functions in a separate file, which makes even more convenient to move functions between the "inline file" and the cpp file without changing the header file with the class definition at all.
@rpgfan3233:
>> The C++ FAQ Lite has an excellent bit regarding this.

That is interesting, but it doesn't actually consider the three possible alternatives. It simply uses the alternative the OP refered to as "keyword next to definition".
My reading of the standard (Sections 7.1.2.3, 9.3.2 and 9.3.3) says that member functions may be specified as inline in the following ways...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct A
{
   // Member function definition provided in the class definition. The
   // function is implicitly inline. That is, it must obey the rules for inline functions
   // and its implicit inline specifier may not be removed via C++ langauge features.
   void f1()
   {
   }
   
   // Function defined outside of class. Inline keyword present on function definition
   // but not the declaration.
   void f2();

   // Function defined outside of class. Inline keyword present on function declaration
   // but not the definition.
   inline void f3();

   // Function defined outside of class. Inline keyword present on both the declaration
   // and definition of the function.
   inline void f4();
};

inline void A::f2() {}

void A::f3() {}

inline void A::f4() {}


To reiterate, all of the above functions ARE inline. (and legal!)

There are two important caveats to inline functions:
1. It is a HINT. The compiler can ignore it. Indeed, in some situations it must ignore it, e.g. recursion.
2. The definition of an inline function must be provided in every translation unit that uses it, even if the compiler does not actually perform the inlining. i.e. out of class member function definitions must still be present in the header file. It cannot be in a separate .cpp file.
Last edited on
closed account (z05DSL3A)
I concede, my interpretation of was a little off. I have always thought that the definition of inline had to be within the definition of the class; it would appear that it only has to be in the header.
Now that we are all on the same page, let's return to the OP's question. Which alternative do you prefer? The three alternatives are equivalent to f2, f3 and f4 in DrDogg's post.

Of course arguments for/against in-class definition are also interesting (f1 vs. {f2,f3,f4}).

BTW: This alternative to f1 is also possible:
1
2
3
struct A {
    inline void f1_() {}
}

The inline keyword is redundant, but some prefer to write it anyway.

As I have already mentioned, I mostly prefer "f2". Generally, I don't like f1, because I like to keep the class definitions as simple as possible. In some cases I have placed the inline function in a separate file, named something like "foo_inline.h", and included at the bottom of "foo.h".
Last edited on
Splendid discussion chaps! Socrates would have been proud. :-)

I suppose, looking at this thread, all that remains to say is that whilst we're all clear on how to use the damn things, we probably shouldn't. The usual health warnings apply:

As with most optimization, don't do it until evidence tells you that it is required, and then only do it where the profiler tells you it will actually improve performance appreciably.

Inlining increases coupling and can decrease performance. And as Herb Sutter says, "Profilers are a lot less good at identifying which inlined functions should not be inlined".
Last edited on
closed account (z05DSL3A)
I would probably lean toward keyword next to the declaration and definition, my reason is that if you have a large header it would be good to know that the definition is in the header file somewhere.

But I always define the function in the class definition, I also use defines to use a non-inline versions for debugging.
>> But I always define the function in the class definition, I also use defines to use a non-inline versions for debugging.

How, exactly, do you accomplish that? A function defined inside the class definition is implicitly inline, and the inline keyword is optional. How can you use defines to non-inline it?

The only way I know of is to use the following pattern, but it requires that inline functions are defined outside the class definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// foo.h
class Foo {
public:
  void f1(); // maybe inline
  void f2(); // not inline
};
#ifndef NO_INLINE
#define INLINE inline
#include "foo_inline.h"
#endif

// foo_inline.h
INLINE void Foo::f1() {}

// foo.cpp
#include "foo.h"
#ifdef NO_INLINE
#define INLINE
#include "foo_inline.h"
#endif
void Foo::f2() {}
Last edited on
closed account (z05DSL3A)
it would generally go along the lines of
1
2
3
4
5
6
7
8
9
10
class Foo
{
...
#ifdef DEBUG
    void test();
#else
    inline void test(){...};
#endif
...
}


with similar guards in the cpp file.

I probably need to rethink this, check out what has changed, it could be that I have picked up bad habits years ago.

To be honest I don’t use inline much.

Last edited on
If you do that, you'll need to maintain two versions! That must be a pain. Anyway, doesn't your compiler/debugger let you step through inline functions in debug mode? I can't remember ever having trouble debugging inline functions.
Pages: 12