Public Group

#### Archived

This topic is now archived and is closed to further replies.

# inline functions

This topic is 5386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
Declare and define the inline function in the header, and don''t put it in the source file.

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.

##### Share on other sites
Declaring functions in the .h file? But that''s so ugly! Is that the only way of making them inline?

yeah?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites
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.

##### Share on other sites
// 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]

##### Share on other sites
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.

• 18
• 11
• 16
• 9
• 50
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999783
×