inline functions

Started by
8 comments, last by madu 20 years, 6 months ago
I have a function declared in a header, and it''s implementation declared in another cpp file. In the main file, i include the header, use the function, and everything works fine. But when I declare the function inline, both in the header and in the implementation, the liker says it can''t find it in the main file. What I am doing wrong?
Advertisement
Declare and define the inline function in the header, and don''t put it in the source file.

Bad:

in.h

inline void somefunc();

in.cpp

inline void somefunc() {}


Good:

in.h

inline void somefunc() {}



Most compilers need the function body at compile-time to perform inlining. They don''t have it if it''s not in the header.
Declaring functions in the .h file? But that''s so ugly! Is that the only way of making them inline?
yeah?
Personally I think declaring inline functions in the header looks neater, and even has a way of looking effecient. The whole point of using inline functions are for defining very short functions in a single line, for a little boost in performance. If you are using so much code that it makes your header file look ugly, than most likely, you shouldn''t be using inlines.
Though not completed, visit My website
I am not using much code, I am using many many relatively small functions. Some of them are 50% faster inline, and some are even almost 100% faster. Well anyway, there is performance gain by declaring them inline. They are heavily commented (the comments are 60% of the code file), so declaring them in the header file is a little nasty.

The other thing to remember is that the inline keyword is more of a guideline to the compiler than an actual requirement. If you declare a function that is the compiler thinks is too big to inline then it will ignore the inline tag. Try to keep you inlined functions short and simple.
you can use __forceinline to makea function inlien nomatter what the compiler thinks of your code. also __fastcall can speed thigns up sometimes not sure when and when not to use that though.
// Headerextern inline void somefunc();// Sourceinline void somefunc(){    std::cout << "Do something..." << std::endl;}      


Edit: Just because something can be done, doesn't always mean it should be done.


DracosX:

Master of the General Protection Fault

[edited by - DracosX on September 22, 2003 11:58:16 PM]
DracosX:Master of the General Protection Fault
Yes, adding the keyword "extern" before "inline" in the header declaration makes the linker work. But there''s one small problem: it doesn''t make it inline anymore. I know this because I have one small function that runs exactly twice as fast with inline than without. Declaring the function in the header seems to work though. So this seems to be the only way. Thank you for your help.

This topic is closed to new replies.

Advertisement