[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: how to separate the implementation of inline functions?
From: |
Guy Harrison |
Subject: |
Re: how to separate the implementation of inline functions? |
Date: |
Mon, 20 Sep 2004 13:59:11 GMT |
User-agent: |
KNode/0.7.7 |
Sean McManus wrote:
>> Not sure what you mean, please clarify with code (I'm gonna be away for
>> for
>> few days).
>>
>> As you'll be aware from C (and "register") "inline" is merely a hint that
>> compiler ought to do "stuff". GCC is better at this than most - a good
>> example of "inline failures" is borland (stonkingly fast compiler but
>> pays for it in this respect). Same applies for C++ I'm afraid - "inline"
>> is today's "register". Aka - the fact "inline" is in the C++ vocabulary
>> means absolutely nothing - compiler is at liberty to do as it will.
>
> I can get inline to work successfully, but only when it's in the .h file
> along with the class declarations, for example
>
> class myClass { // this works
> inline void myFunc() { ... } //... do something inline
> };
Yes. The definition needs to be available to the compilation unit that's
attempting to make use of it.
> // the code below does not work in g++, but it should as far as I know
> class myClass { // in the .h
> inline void myFunc();
> };
^^^
That's okay but as Robert states, you still need the definition.
> void myClass::myFunc() { // in .cpp
> // ...do something inline
> }
^^^
Think like a template here. Move it into the header and add the "inline"
back in.
> It gives the warning message
> warning: inline function `void myClass::myFunc()' used but never defined
>
> undefined reference to `myClass::myFunc()'
>
> It looks like this is causing a linker error.
Correct. Your mileage will vary depending on gcc version I think. Even if
one rewrites this the "traditional (inlines in the foo declaration)" way...
===
<c.hpp>
#ifndef C_HPP
#define C_HPP C_HPP
struct foo {
inline void bar(int&);
};
inline void
foo::bar(int&x)
{++x;}
#endif //C_HPP
</c.hpp>
===
<c.cpp>
#include "c.hpp"
int
main()
{foo f;
int x(0);
f.bar(x);
return x;
}
</c.cpp>
===
...no inlining appears to occur (here with, hmm... 3.3.4) until such time as
some kind of optimisation is applied. Indeed...
struct foo {
/*inline*/ void bar(int&x) {++x;}
};
...generates the same code reguardless of inline commented out or not - it's
"-O" that's the trigger (there may be a specific flag, I've not bothered to
look). Perhaps I contrived an odd example but I suspect not as "outline"
code is more preferable to debug.