Template Class Member Definitions in Headers?!?

M

Markus Kern

Hi!

I've been messing around with a template class. As usual, I do not
want to make the definition of my functions public, but only its
declaration. This was the concept of seperating header-files from the
definitions, wasn't it? So one can offer the headers all along with the
binary objects.

This, of course, fails when using templates, since the linker does not
know what type was used when the processing the template.

Refer to e.g:

http://groups.google.de/group/micro...+template+class&rnum=8&hl=de#a12c8268753b6939



Isn't that completely against the concept of using
header/definition-files?

Thanks for any comments.
 
M

Markus Kern

O Jesus, there's even more:

How do I provide a binary-library of my template class? Do I need to
make objects out of the header-file? Or would I skip any header file,
put everything in a *.cpp and #include "aaa.cpp"?!?

I'm really very puzzled...
 
K

Kai-Uwe Bux

Markus said:
O Jesus, there's even more:

How do I provide a binary-library of my template class? Do I need to
make objects out of the header-file?
Huh?


Or would I skip any header file,
put everything in a *.cpp and #include "aaa.cpp"?!?

That's what I do: I ditched header files altogether.

I'm really very puzzled...

You could go for a compiler that supports the export keyword.


Best

Kai-Uwe Bux
 
J

Jens Theisen

Markus said:
Hi!

I've been messing around with a template class. As usual, I do not
want to make the definition of my functions public, but only its
declaration. This was the concept of seperating header-files from the
definitions, wasn't it?

The concept of that was to speed up compile time by compiling a
translation unit at a time. I don't think they are there to hide logic
from unauthorised eyes, if that's what you mean.
Isn't that completely against the concept of using
header/definition-files?

Indeed it has some problems, like compile and link time being increased,
sometimes drastically. It's not too easy to think of an alternative though.

What you want is the "separation model" for templates, which most
compilers don't support (and has it's problems as I've heard).

Jens
 
T

Thomas Tutone

Markus said:
I've been messing around with a template class. As usual, I do not
want to make the definition of my functions public, but only its
declaration. This was the concept of seperating header-files from the
definitions, wasn't it? So one can offer the headers all along with the
binary objects.

This, of course, fails when using templates, since the linker does not
know what type was used when the processing the template.

Refer to e.g:

http://groups.google.de/group/micro...+template+class&rnum=8&hl=de#a12c8268753b6939



Isn't that completely against the concept of using
header/definition-files?

Have you reviewed the FAQ? It has quite a bit of explanation about
these issues.

http://www.parashift.com/c++-faq-lite/templates.html

If you still have questions after reviewing it, I'm sure someone here
can help.

Best regards,

Tom
 
T

Thomas Tutone

Markus said:
I've been messing around with a template class. As usual, I do not
want to make the definition of my functions public, but only its
declaration. This was the concept of seperating header-files from the
definitions, wasn't it? So one can offer the headers all along with the
binary objects.

This, of course, fails when using templates, since the linker does not
know what type was used when the processing the template.

Refer to e.g:

http://groups.google.de/group/micro...+template+class&rnum=8&hl=de#a12c8268753b6939



Isn't that completely against the concept of using
header/definition-files?

Have you reviewed the FAQ? It has quite a bit of explanation about
these issues.

http://www.parashift.com/c++-faq-lite/templates.html

If you still have questions after reviewing it, I'm sure someone here
can help.

Best regards,

Tom
 
M

Markus Kern

Have you reviewed the FAQ? It has quite a bit of explanation about
these issues.

Yes, I have read the FAQ. It suggest just what I tried to avoid, which
is:

1. Put the definitions into the header (worst solution).
2. Include the definition file (which suggests that the source code of
the definitions needs to be available and, thus, prevents me from
hiding it).
3. Create a new file that includes the definition file in case the
definition file is not writable (just the same inconvenience as in 2.)

I have no more problem in realizing my project. I will, however,
reject the use of templates, and, instead, overload the class. I know,
Mr. B. Stroustrup is going to cut my throat as soon as he meets me, but
I desperately need to hide the definitions...

Thanks anyway,

yours
M.K
 
M

Markus Kern

Jens said:
The concept of that was to speed up compile time by compiling a
translation unit at a time. I don't think they are there to hide logic
from unauthorised eyes, if that's what you mean.

O.k, this makes sense. In this case, including template definitions is
completely legal.
Indeed it has some problems, like compile and link time being increased,
sometimes drastically. It's not too easy to think of an alternative though.

What you want is the "separation model" for templates, which most
compilers don't support (and has it's problems as I've heard).

Thank you for your comment, anyway. This was what I expected, but just
did not want to believe ;-).

Yours
M.K.
 
I

I V

Yes, I have read the FAQ. It suggest just what I tried to avoid, which
is:

1. Put the definitions into the header (worst solution).
2. Include the definition file (which suggests that the source code of
the definitions needs to be available and, thus, prevents me from
hiding it).
3. Create a new file that includes the definition file in case the
definition file is not writable (just the same inconvenience as in 2.)

The FAQ also suggests using explicit instantiation, which may work for you:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15

---QUOTE---
The other solution is to leave the definition of the template function in the
..cpp file and simply add the line template class Foo<int>; to that file:

// File "Foo.cpp"
#include <iostream>
#include "Foo.h"

...definition of Foo<T>::f() is unchanged -- see above...
...definition of Foo<T>::g() is unchanged -- see above...

template class Foo<int>;

---END QUOTE---

If your users are only going to need to instantiate the class for a
defined number of types, you can explicitly instantiate it for those types
in the .cpp file, and link it in as normal.
I have no more problem in realizing my project. I will, however,
reject the use of templates, and, instead, overload the class. I know,
Mr. B. Stroustrup is going to cut my throat as soon as he meets me, but
I desperately need to hide the definitions...

Why do you need to hide the definitions, BTW?
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top