Templates for syntatic niceness only, does it waste space?

C

collection60

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?
 
U

Uday Bidkar

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
 
M

mlimber

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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

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.
 
C

collection60

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?
 
B

Bo Persson

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
 
O

Old Wolf

Uday said:
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
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.
 
M

mlimber

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
 
M

Michiel.Salters

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top