473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
2 1531
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bolin | last post by:
I am frustrated as I am trying to design a toy image library. I want the user to write mul(im, 2) if s/he wants to multiply the image by 2, and mul(im1, im2) if s/he wants to multiply...
3
by: Ruben Campos | last post by:
Greetings. Some time ago, I had writing a CVector <T, N> class, which implements an algebraic vector of arbitrary both dimension and scalar type. First, I defined the interface for the generic...
14
by: Joseph Turian | last post by:
Let's say I have a function template as follows: template<class T, class U> foo { ... } However, I'd like to have different behavior if T == U. How can I check that?
5
by: Andrei Tarassov | last post by:
Hi! I would like to know if it is possible to implement something like this: template <class X, class Y> X void func(X, Y) { ... } template <class X, class Y> Y void func(X, Y) { ... }
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
6
by: petschy | last post by:
hello all, i'm trying to write a trait class (to generate an uint hash code for a type): template<typename T> struct S { static unsigned int f(const T& t_); };
12
by: sam_cit | last post by:
Hi Everyone, I have few questions on inline functions, when i declare a function as inline, is it for sure that the compiler would replace the function call with the actual body of the function?...
2
by: aaragon | last post by:
Hi everyone, I would like to create a very simple function: // header file inline void point_map() { PointMap pointMap = get(vertex_point_t(), g); } // main.cpp
3
by: =?iso-8859-1?B?Tm9yZGz2dw==?= | last post by:
Hey there, C++ Coders! I am learning multi-threading with boost and have come up with the following code example shown below. This example implements a test of the producer-consumer design...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.