473,545 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

templates and their compilation

hello,
this is my first post in a newsgroup, i hope i do everything right :)

first of all, i am sure there have been a lot of "are templates slow?"
questions around, but i think what i would like to know is not that
general. furthermore, what i ask here may have hardly any impact on
the generated code size or performance; i am not trying to micro-
optimize anything, i am just curious how templates are compiled by
modern compilers of various vendors. i know that the compilers do not
behave the same, so whenever i speak of "compilers" , i welcome any
information that is either special to one compiler or widely adopted
by most compilers.
by the way, i have done some searching prior to posting this, but
unfortunately i could not find satisfying answers. i would have done
some test myself if i knew assembler well enough to extract any
information out of the compiler generated code.

Q1)
i have heard that some compilers do not inline template functions or
classes, but i guess this information is quite outdated and modern
compilers inline them just as normal functions/classes, don't they?

Q2)
assume there is a template (member) function funct that has one (or
more) template arguments. funct does a lot of work, but the places it
actually makes any use of the template parameter are relatively rare.
so, all instatiations of funct have a lot of code in common, the
generic code is rather small. do compilers somehow "merge" that common
code so it is not duplicated everytime a new instantiation of funct
occurs?

Q3)
i am currently reading "Modern C++ Design" by Alexandrescu. There, he
demonstrates various techniques to evaluate expressions (might be the
wrong term in this case) at compile time. as an example, i wondered in
chapter 2.4 (here is the code in question: http://rafb.net/p/C9RwGV80.html
) how far compilers really optimizes this template.
if, for example, i instantiate NiftyContainer as follows:
NiftyContainer< int, falsex;
Here, the compiler could "redirect" every call to x.DoSomething(i nt*)
directly to x.DoSomething(i nt*, Int2Type<false> ), without calling
x.DoSomething(i nt*) to avoid the calling overhead. do compilers do
that in practise or is that too complicated or even impossible for
more complex real-world code?

Q4)
as in the question above, Alexandrescu uses overloaded functions with
unused dummy arguments. if the compiler has access to the full source
code of the function, it may note that the argument really is unused
(and thus unnamed) and eliminate the overhead of passing the argument.
however, if the definition of the function is not accessible (for
example, it is "hidden" in a closed-source library), the compiler
cannot know whether the argument is used, so it has to pass it. how do
compilers behave in these situations?

Q5)
Alexandresu says that policy classes are either to be contained within
or inherited by the host classes, which makes sense. since only some
policies (and no traits, if i understood that correctly) have any
member data, the compiler could optimize the need to construct/
destruct the parent policy (in case of inheritance) or the member
instance (in case of containment) away. maybe, but i am not sure
whether this is possible for containment, it even could avoid
allocating the minimum size of one byte for the empty class; when used
with inheritance, this should be no problem, is it?

i hope i have stated my questions in an understandable way :)

thank you for possible answers in advance

Aug 14 '07 #1
6 1444
pl*****@gmail.c om wrote:
....
Q1)
i have heard that some compilers do not inline template functions or
classes, but i guess this information is quite outdated and modern
compilers inline them just as normal functions/classes, don't they?
You can easily test thus myth. Most compilers I have used recently do a
very good job of inlining template funcs when optimization is turned on.
>
Q2)
assume there is a template (member) function funct that has one (or
more) template arguments. funct does a lot of work, but the places it
actually makes any use of the template parameter are relatively rare.
so, all instatiations of funct have a lot of code in common, the
generic code is rather small. do compilers somehow "merge" that common
code so it is not duplicated everytime a new instantiation of funct
occurs?
I have yet to see a compiler merge generic code. However, the one time
(yes only once) where it made any sense for me to refactor the template
function, it was quite trivial to do this. In any case, code size, even
for multiple instantiations of "big" functions has never been a concern
for regular applications.
>
Q3)
i am currently reading "Modern C++ Design" by Alexandrescu. There, he
demonstrates various techniques to evaluate expressions (might be the
wrong term in this case) at compile time. as an example, i wondered in
chapter 2.4 (here is the code in question: http://rafb.net/p/C9RwGV80.html
) how far compilers really optimizes this template.
if, for example, i instantiate NiftyContainer as follows:
NiftyContainer< int, falsex;
Here, the compiler could "redirect" every call to x.DoSomething(i nt*)
directly to x.DoSomething(i nt*, Int2Type<false> ), without calling
x.DoSomething(i nt*) to avoid the calling overhead. do compilers do
that in practise or is that too complicated or even impossible for
more complex real-world code?
I didn't look at the code, but it's quite possible that the compiler
will make the optimization if it makes sense.
>
Q4)
as in the question above, Alexandrescu uses overloaded functions with
unused dummy arguments. if the compiler has access to the full source
code of the function, it may note that the argument really is unused
(and thus unnamed) and eliminate the overhead of passing the argument.
however, if the definition of the function is not accessible (for
example, it is "hidden" in a closed-source library), the compiler
cannot know whether the argument is used, so it has to pass it. how do
compilers behave in these situations?
Nothing different here to regular functions.
>
Q5)
Alexandresu says that policy classes are either to be contained within
or inherited by the host classes, which makes sense. since only some
policies (and no traits, if i understood that correctly) have any
member data, the compiler could optimize the need to construct/
destruct the parent policy (in case of inheritance) or the member
instance (in case of containment) away. maybe, but i am not sure
whether this is possible for containment, it even could avoid
allocating the minimum size of one byte for the empty class; when used
with inheritance, this should be no problem, is it?
Most compilers now implement the empty base class optimization so there
is no inheritance overhead for empty classes.
>
i hope i have stated my questions in an understandable way :)

thank you for possible answers in advance
Aug 14 '07 #2
Q4)
Nothing different here to regular functions.
i may have expressed my question poorly; i did not intend to make this
question specific to template functions, i am curious to know how
compilers handle this in general.
Q5)
Most compilers now implement the empty base class optimization so there
is no inheritance overhead for empty classes.
so, inheriting from policies instead of containing them as members
would save me a bit (actually, a byte) of memory and maybe some CPU
instructions? i kind of feel biased towards the containment approach
because i have a, perhaps irrational, "fear" of multiple inheritance,
which might be needed if i decide to inherit from one or more
policies, maybe in addition to an oridinary base class.

thank you for your reply

Aug 14 '07 #3
On Aug 15, 6:14 am, plee...@gmail.c om wrote:
Q4)
Nothing different here to regular functions.

i may have expressed my question poorly; i did not intend to make this
question specific to template functions, i am curious to know how
compilers handle this in general.
Your original analysis was correct.
Q5)
Most compilers now implement the empty base class optimization so there
is no inheritance overhead for empty classes.

so, inheriting from policies instead of containing them as members
would save me a bit (actually, a byte) of memory and maybe some CPU
instructions? i kind of feel biased towards the containment approach
because i have a, perhaps irrational, "fear" of multiple inheritance,
which might be needed if i decide to inherit from one or more
policies, maybe in addition to an oridinary base class.
It's not good to second-guess the compiler optimisations, i.e. some
compilers might not waste space for empty contained objects (for which
the address isn't taken), and if they don't implement this
optimisation now they might do so in future. It has no practical
import, so you shouldn't let it dictate your coding. And how can we
respond to your irrational fears? If it was the '60s, maybe someone
would offer you a hug...? :-) Don't let the Java/C# nonsense about
multiple inheritance pollute your thinking - more a case of making up
criticisms for whatever they left out.

Cheers,

Tony

Aug 15 '07 #4
joe
On Aug 14, 5:14 pm, plee...@gmail.c om wrote:
so, inheriting from policies instead of containing them as members
would save me a bit (actually, a byte) of memory and maybe some CPU
instructions? i kind of feel biased towards the containment approach
because i have a, perhaps irrational, "fear" of multiple inheritance,
which might be needed if i decide to inherit from one or more
policies, maybe in addition to an oridinary base class.

thank you for your reply
As was mentioned else where, don't be afraid of multiple inheritance.
However,
if your policy has no state, then you don't need to either inherit nor
to have a
member. All of your methods can be invoked as Policy::method( ) where
needed. After all,
if there is no state data, they might as well be static methods in the
policy.

joe
Aug 15 '07 #5
if there is no state data, they might as well be static methods in the
policy.
simple as it is, i have not even thought about this possibility ;)

thank you joe and Tony for your posts

still, Q3 is not really answered now. the optimization seems possible
to me, but i am not sure whether compilers detect this opportunity or
if the standard even allows this.
hopefully someone who knows the definite answer for any compiler will
post :>

Aug 15 '07 #6
Hi!

pl*****@gmail.c om schrieb:
still, Q3 is not really answered now. the optimization seems possible
to me, but i am not sure whether compilers detect this opportunity or
if the standard even allows this.
The compiler will inline the first call. So, yes, this is optimized away
on most compiliers in release mode.

Frank
Aug 15 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2361
by: Allan Bruce | last post by:
I have had a look through the FAQ and found that if I am using a class template then I need an argument list. I have tried to add this but it is not quite working - i.e. it doesnt compile. My code is below, could somebody please point me in the right direction? Thanks Allan #include <iostream>
5
1410
by: Ray Gardener | last post by:
Would templates be necessary if C++ had all numeric types inherit from a base "number" class and had all types inherit from a base "object" class? (Sorry if this has already been discussed to death) Ray
3
1556
by: Ajay Daptardar | last post by:
Hi, I have the following problem. Consider this: // codec.h template <class T> class codec { public: codec(T val); private:
25
3299
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the same subject in clc++m to get the more of the context. Ted
5
1766
by: josh | last post by:
Hi if I want only write a template function in a non-template class I have an error: in Array.h class Array { public: Array() {} ~Array() {}
2
3871
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
104
4463
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also...
1
1431
by: Rahul | last post by:
Hi Everyone, I was looking at the link, http://www.comeaucomputing.com/4.0/docs/userman/export.html and it looks like the basic purpose of exporting templates is make sure that they are visible in other compilation units for the compiler to perform instantiation... is this because template instantiation is something done at compile...
26
231
by: puzzlecracker | last post by:
Team, C++ has been around since 1986, why templates are still regarded is a new feature by most compiler vendors and not fully supported (for example export feature). Look at other popular languages -- say Java, CSharp --and templates , also known as generics, are fully implemented and supported in latest releases. Yes, in C++ they are...
0
7499
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.