Connecting Tech Pros Worldwide Forums | Help | Site Map

Templates for syntatic niceness only, does it waste space?

collection60@hotmail.com
Guest
 
Posts: n/a
#1: Jul 23 '06
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?


Uday Bidkar
Guest
 
Posts: n/a
#2: Jul 23 '06

re: Templates for syntatic niceness only, does it waste space?


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

collection60@hotmail.com wrote:
Quote:
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?
mlimber
Guest
 
Posts: n/a
#3: Jul 23 '06

re: Templates for syntatic niceness only, does it waste space?


collection60@hotmail.com wrote:
Quote:
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

Julián Albo
Guest
 
Posts: n/a
#4: Jul 23 '06

re: Templates for syntatic niceness only, does it waste space?


collection60@hotmail.com wrote:
Quote:
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
collection60@hotmail.com
Guest
 
Posts: n/a
#5: Jul 23 '06

re: Templates for syntatic niceness only, does it waste space?



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?
Quote:
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
Bo Persson
Guest
 
Posts: n/a
#6: Jul 23 '06

re: Templates for syntatic niceness only, does it waste space?



<collection60@hotmail.comskrev i meddelandet
news:1153658347.140454.53520@i42g2000cwa.googlegro ups.com...
Quote:
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


Old Wolf
Guest
 
Posts: n/a
#7: Jul 24 '06

re: Templates for syntatic niceness only, does it waste space?


Uday Bidkar wrote:
Quote:
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).
Quote:
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.

mlimber
Guest
 
Posts: n/a
#8: Jul 24 '06

re: Templates for syntatic niceness only, does it waste space?


collection60@hotmail.com wrote:
Quote:
Quote:
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

Michiel.Salters@tomtom.com
Guest
 
Posts: n/a
#9: Jul 24 '06

re: Templates for syntatic niceness only, does it waste space?


collection60@hotmail.com wrote:
Quote:
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

Closed Thread