Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why template functions in boost lib are all inlined?

Reply
Thread Tools

why template functions in boost lib are all inlined?

 
 
newbie
Guest
Posts: n/a
 
      04-24-2007
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

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      04-25-2007
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).
 
Reply With Quote
 
 
 
 
Dave Rahardja
Guest
Posts: n/a
 
      04-27-2007
On Wed, 25 Apr 2007 00:06:04 GMT, Mark P
<(E-Mail Removed)> wrote:

>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Problem using boost::bind() with template functions having arguments =?iso-8859-1?B?Tm9yZGz2dw==?= C++ 3 08-21-2007 09:51 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments