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.
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();
inlinevoid fun(){...}
};
Void Base:: noinline()
{...}
@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.
@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:
>> 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.
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".
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.
inlinevoid f3();
// Function defined outside of class. Inline keyword present on both the declaration
// and definition of the function.
inlinevoid f4();
};
inlinevoid A::f2() {}
void A::f3() {}
inlinevoid 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.
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 {
inlinevoid 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".
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".
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:
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.