Boost logo

Ublas :

Subject: Re: [ublas] Is there a special reason to use inline before template functions?
From: Andreas Klöckner (lists_at_[hidden])
Date: 2008-12-08 14:07:35


On Sonntag 07 Dezember 2008, Thomas Klimpel wrote:
> Hi,
>
> in boost, many template functions (but not all) use inline.
> (Example from boost/accumulators/framework/accumulator_set.hpp that uses
> inline: template<typename Args>
> inline accumulator_visitor<Args> const make_accumulator_visitor(Args
> const &args) {
> return accumulator_visitor<Args>(args);
> }
>
> Example from boost/algorithm/minmax.hpp hat doesn't use inline:
> template <typename T>
> tuple< T const&, T const& >
> minmax(T const& a, T const& b) {
> return (b<a) ? make_tuple(cref(b),cref(a)) :
> make_tuple(cref(a),cref(b)); }
> )
>
> 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?

Two points:

- Even if you say "inline", the compiler may well decide to not inline. In
that sense, it's just a hint.

- If the compiler decides to not inline, then multiple definitions of that
function will be found by the linker. This violates the ODR and the linker
will complain. *Unless* that function is declared inline, which makes it ok.

So it actually has a purpose, even though it's not what its name suggests.

Andreas