473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templates for syntatic niceness only, does it waste space?

Hi people,

I want to use templates, for syntatic niceness only, not for doing
similar operations on different types. The types I'm manipulating are
basically just pointers. So if it's a pointer to a Fred or a pointer to
a long, it makes no difference.

So, I want the exact same code, to be running on different types. So
even if I used this template on 1000 different types, I'd want only one
copy of that code in my executable, I do not want the size of my
executable to go up with the number of types used by this template.

Is that how it works? Can I get syntactic niceness without getting code
bloat?

Jul 23 '06 #1
8 1295
Hi

In case of templates, a separate code is generated for each type being
used in your code.
In fact, no code is generated if you dont use the template in your code
at all but just declare it. i.e., the code
template<typename T>

void Foo( T t )
{
These are garbage lines,
aaaa
bbbb
cccc
/+*-
}
int main()
{
return 0 ;
}

will compile successfuly as you are calling Foo nowhere

So, the size of object files generated depends upon the number of
types you use the template for and hence the size of the executable.

Regards,
Uday Bidkar

co**********@hotmail.com wrote:
Hi people,

I want to use templates, for syntatic niceness only, not for doing
similar operations on different types. The types I'm manipulating are
basically just pointers. So if it's a pointer to a Fred or a pointer to
a long, it makes no difference.

So, I want the exact same code, to be running on different types. So
even if I used this template on 1000 different types, I'd want only one
copy of that code in my executable, I do not want the size of my
executable to go up with the number of types used by this template.

Is that how it works? Can I get syntactic niceness without getting code
bloat?
Jul 23 '06 #2
co**********@hotmail.com wrote:
Hi people,

I want to use templates, for syntatic niceness only, not for doing
similar operations on different types. The types I'm manipulating are
basically just pointers. So if it's a pointer to a Fred or a pointer to
a long, it makes no difference.

So, I want the exact same code, to be running on different types. So
even if I used this template on 1000 different types, I'd want only one
copy of that code in my executable, I do not want the size of my
executable to go up with the number of types used by this template.

Is that how it works? Can I get syntactic niceness without getting code
bloat?
If you're using the same code to manipulate many disparate pointer
types, chances are you're not doing much significant with them (e.g.,
you aren't using the interface for Fred, using them polymorphically,
etc.). So just declare the function inline, and things will likely be
the same (since "inline" is only a hint) as if you wrote that pointer
manipulating code in each place where you call the function.

Cheers! --M

Jul 23 '06 #3
co**********@hotmail.com wrote:
I want to use templates, for syntatic niceness only, not for doing
similar operations on different types. The types I'm manipulating are
basically just pointers. So if it's a pointer to a Fred or a pointer to
a long, it makes no difference.

So, I want the exact same code, to be running on different types. So
even if I used this template on 1000 different types, I'd want only one
copy of that code in my executable, I do not want the size of my
executable to go up with the number of types used by this template.
You can specialize your template for void *, an make a partially
specialization for pointer types that uses it. "The C++ programming
language" shows how to do that.

Or write a non-templated class or function for void *, and use it, or
inherit from it, in the template.

--
Salu2
Jul 23 '06 #4

Well, think of the hash_map class.

Let's say I have a hash_map relating strings to long*, another for
strings to Fred* and another for strings to X*.

Well, the code is entirely interchangable. I'd hope that the compiler
knows this. Know it seems that it doesn't?
If you're using the same code to manipulate many disparate pointer
types, chances are you're not doing much significant with them (e.g.,
you aren't using the interface for Fred, using them polymorphically,
etc.). So just declare the function inline, and things will likely be
the same (since "inline" is only a hint) as if you wrote that pointer
manipulating code in each place where you call the function.

Cheers! --M
Jul 23 '06 #5

<co**********@hotmail.comskrev i meddelandet
news:11*********************@i42g2000cwa.googlegro ups.com...
Hi people,

I want to use templates, for syntatic niceness only, not for doing
similar operations on different types. The types I'm manipulating
are
basically just pointers. So if it's a pointer to a Fred or a pointer
to
a long, it makes no difference.

So, I want the exact same code, to be running on different types. So
even if I used this template on 1000 different types, I'd want only
one
copy of that code in my executable, I do not want the size of my
executable to go up with the number of types used by this template.

Is that how it works? Can I get syntactic niceness without getting
code
bloat?
Some compilers will recognize that the code is identical for several
types, and merge the code. Some will not.

This is a typical quality of implementation thing, the language
standard doesn't say what will happen.
Bo Persson
Jul 23 '06 #6
Uday Bidkar wrote:
Hi

In case of templates, a separate code is generated for each type being
used in your code.
Not necessarily; most compilers will generate the same code
for Foo<longas for Foo<int(if they have int and long both as
the same size).
In fact, no code is generated if you dont use the template in your code
at all but just declare it. i.e., the code
template<typename T>

void Foo( T t )
{
These are garbage lines,
aaaa
bbbb
cccc
/+*-
}
int main()
{
return 0 ;
}

will compile successfuly as you are calling Foo nowhere
Not true. The code for Foo() must be syntactically correct.
(Your particular compiler might compile it, but others won't).

Imagine what would happen if the garbage lines contained
a "}" character. How does the compiler know where the end
of the function is? You can' t just search for braces; this is
a valid function:

void foo()
{
X(})
}

Hopefully you can now see some of the reasons why the code
has to follow correct C++ syntax.

Jul 24 '06 #7
co**********@hotmail.com wrote:
If you're using the same code to manipulate many disparate pointer
types, chances are you're not doing much significant with them (e.g.,
you aren't using the interface for Fred, using them polymorphically,
etc.). So just declare the function inline, and things will likely be
the same (since "inline" is only a hint) as if you wrote that pointer
manipulating code in each place where you call the function.

Well, think of the hash_map class.

Let's say I have a hash_map relating strings to long*, another for
strings to Fred* and another for strings to X*.

Well, the code is entirely interchangable. I'd hope that the compiler
knows this. Know it seems that it doesn't?
(It's the custom here to put your reply inline or after the post you
are responding to. I fixed yours here.)

As Bo Persson said, it is more of a QoI issue as to whether the
compiler will actively combine code for classes and functions generated
from templates, but even with the STL, my point stands: most STL
containers' member functions (and those of soon-to-be STL containers
like hash_map, aka std::tr1::unordered_map) are inline by default,
which will often amount to the same thing as combining code.

Cheers! --M

Jul 24 '06 #8
co**********@hotmail.com wrote:
Hi people,

I want to use templates, for syntatic niceness only, not for doing
similar operations on different types. The types I'm manipulating are
basically just pointers. So if it's a pointer to a Fred or a pointer to
a long, it makes no difference.

So, I want the exact same code, to be running on different types. So
even if I used this template on 1000 different types, I'd want only one
copy of that code in my executable, I do not want the size of my
executable to go up with the number of types used by this template.
Typically, an optimizer looks at the bytes generated. If two functions
have
the same bytes, they usually can have the same address. Of course, this
requires the optimizer to be run - "debug" builds will usually keep
them
distinct so you can understand what's going on.

But what's stopping you from trying? If the compiler fails, you can
always add
the non-nice form yourself and make the syntactically nice versions
into
one-line inlined wrappers. Same result, but with more work for you.

HTH,
Michiel Salters

Jul 24 '06 #9

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

Similar topics

22
2143
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
1
1735
by: Arne Petersen | last post by:
Hy, I've got a problem with member function templates compiled into libraries. I'm trying to get a library collection (coded for GNU gcc, where its compiled completly) being compiled on Visual...
0
401
by: Jon Slaughter | last post by:
For anyone, though I doubt anyone, who as been following my recent adventures with trying to do recursive templates it seems that I have finaly got everything to work out right. It actually ends...
2
1612
by: jimbo_vr5 | last post by:
Hey I think i've figured out the idea behind apply-templates. But going through the tutorial on <http://www.w3schools.com/xsl/xsl_apply_templates.asp> theres simply just something that i dont...
18
3666
by: jacob navia | last post by:
In C, we have read-only memory (const), read/write memory (normal data), and write only memory. Let's look at the third one in more detail. Write only memory is a piece of RAM that can only...
25
3287
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...
28
2595
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
14
2164
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
3
2040
by: tschwartz | last post by:
I'm trying to write a stylesheet which removes nodes which are empty as a result of other template processing. For example, given the following xml, I'd like to: - remove all "b" elements -...
0
7202
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
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5578
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,...
1
5014
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
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 ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.