why template functions in boost lib are all inlined?

N

newbie

I happened to read boost library code and realized that most (the part
I read) functions are inlined like:

template <class Key>
inline void Foo(const Key& k) {
...
...
}

Is there a strong reasoning behind this?

Thanks
 
M

Mark P

newbie said:
I happened to read boost library code and realized that most (the part
I read) functions are inlined like:

template <class Key>
inline void Foo(const Key& k) {
...
...
}

Is there a strong reasoning behind this?

Thanks

It's not just boost. Generally template functions are inlined (often
implicitly, without the use of the "inline" keyword). Since all of a
program's template instances are not generally known until all
translation units have been compiled, the template function definition
needs to be accessible wherever the function is instantiated. For an
alternative approach look up the "export" keyword, but know that this
feature is not widely supported (Comeau, I believe, being the well-known
exception to this).
 
D

Dave Rahardja

The same reasoning that would lead one to inline non-templated functions.
It's not just boost. Generally template functions are inlined (often
implicitly, without the use of the "inline" keyword). Since all of a
program's template instances are not generally known until all
translation units have been compiled, the template function definition
needs to be accessible wherever the function is instantiated. For an
alternative approach look up the "export" keyword, but know that this
feature is not widely supported (Comeau, I believe, being the well-known
exception to this).

If you mean that templated functions are /always/ inlined, then you would be
mistaken. The compiler/linker is required to gather all instantiations of a
templated function, and consolidate them into one implementation, which may
not be inlined.

If you mean that templated functions are /more likely/ to be automatically
inlined, then you're probably technically correct. A very small templated
function would likely be automatically inlined, while a very small
non-templated function might be impossible to inline because its
implementation may not be visible to the caller. However, I don't think
compilers generally favor templated functions for inlining just because
they're templated. Note that taking the address of the function, etc. will
still force a non-inlined instance to be created.

With Whole Program Optimization available on many compilers these days, the
inlining potential of templated and non-templated functions are probably
exactly the same.

-dr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top