issue with header file

Pages: 12
closed account (4Gb4jE8b)
wait... now i'm confused. We just had this whole discussion about not writing the definition in the header file, and now i read up on inline functions http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.6 and it's saying that you NEED to define the function in the header file or else you'll get a linker error.

is it because of this

"If the compiler inline-expands the call to g(), all those memory operations could vanish. The registers wouldn't need to get written or read since there wouldn't be a function call, and the parameters wouldn't need to get written or read since the optimizer would know they're already in registers. "

that it's directly including the function from the header into the function call?
Yes, inline and template functions go in headers.
My guess:
when you use inline, is like not having a function call but a copy paste of the code
And with templates, the code is generated in compilation time (the template is just a recipe) so you don't get redefinition.

However you still have the compilation overhead.
But with templates you could use the pre-instantiation trick. (also called explicit template instantiation)
1
2
3
4
5
6
//template.cpp
/*[function definitions]
or #include "template.hpp" (implementation)*/

#include "template.h"
template class Foo<int>;
Last edited on
Topic archived. No new replies allowed.
Pages: 12