473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Template Class Member Definitions in Headers?!?


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/micros...2c8268753b6939

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

Thanks for any comments.

Aug 26 '06 #1
9 1710

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

Aug 26 '06 #2
Markus Kern wrote:
>
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
Aug 26 '06 #3
Markus Kern wrote:
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
Aug 26 '06 #4
Markus Kern wrote:
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/micros...2c8268753b6939

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

Aug 26 '06 #5
Markus Kern wrote:
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/micros...2c8268753b6939

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

Aug 26 '06 #6

Kai-Uwe Bux schrieb:
You could go for a compiler that supports the export keyword.
(Un)fortunately, for me there's no alternative to Intel's compiler.

Aug 26 '06 #7
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

Aug 26 '06 #8

Jens Theisen schrieb:
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.
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).
Thank you for your comment, anyway. This was what I expected, but just
did not want to believe ;-).

Yours
M.K.

Aug 26 '06 #9
I V
On Sat, 26 Aug 2006 08:52:21 -0700, Markus Kern wrote:
>
>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.)
The FAQ also suggests using explicit instantiation, which may work for you:

http://www.parashift.com/c++-faq-lit...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?

Aug 26 '06 #10

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

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: Patrick Guio | last post by:
Hi, I have trouble to compile the following piece of code with g++3.4 but not with earlier version // Foo.h template<typename T> class Foo { public:
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.