Boost logo

Boost :

Subject: Re: [boost] Is there a special reason to use inline before template functions?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2008-12-08 13:09:27


Thomas Klimpel wrote:
> Hi,
>
> in boost, many template functions (but not all) use inline.
>
> Is there a special reason why inline is used for template functions? I would expect that current compilers know better when inlining a function provides a benefit than the programmer. So I guess there is a better reason for using the inline keyword before template function than just speculative performance optimization, but I can't figure it out. Can anybody help me?

To my understanding, template functions are no different from the
regular (non-template) functions. If it's not marked up as inline, it is
not inline, and compilers may not inline it unless some aggressive
optimization is on (like -finline-functions on GCC or -Ob2 on MSVC, IIRC).

However, one significant difference should be noted. Unlike regular
functions, non-inline template functions do not cause linking errors if
instantiated from different TUs. That's why the inline keyword is often
simply forgotten when it actually should be there.

> Most libs only prepend inline before template functions, but ublas prepends nearly every member function with the macro "BOOST_UBLAS_INLINE", which expands to "inline". Is there a reason why ublas uses this macro?

I didn't look at the code, but if these member functions are declared in
the class definition, they are already inline and this keyword is not
needed. If not, then this is probably to get rid of the aforementioned
linking problems.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk