467,081 Members | 958 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

why template functions in boost lib are all inlined?

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

Apr 24 '07 #1
  • viewed: 1295
Share:
2 Replies
newbie wrote:
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).
Apr 25 '07 #2
On Wed, 25 Apr 2007 00:06:04 GMT, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fmwrote:
>newbie wrote:
>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?
The same reasoning that would lead one to inline non-templated functions.
>>
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).
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
Apr 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bolin | last post: by
14 posts views Thread by Joseph Turian | last post: by
5 posts views Thread by Andrei Tarassov | last post: by
19 posts views Thread by aaragon | last post: by
6 posts views Thread by petschy | last post: by
12 posts views Thread by sam_cit@yahoo.co.in | last post: by
2 posts views Thread by aaragon | last post: by
3 posts views Thread by =?iso-8859-1?B?Tm9yZGz2dw==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.