473,473 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

header file for template functions/classes

Hello,

I was told that if I have a template class or template function, then
the definitions must be put into the header file where I put the
declarations. On the other hand, it is generally good to put source
code and declaration into separate files (.h head filer and .cpp
source file). What can I do to template functions/classes? Do I have
to put them all into one header file?

Thanks,
Jess

May 17 '07 #1
14 11712
Jess wrote:
:: Hello,
::
:: I was told that if I have a template class or template function,
:: then the definitions must be put into the header file where I put
:: the declarations. On the other hand, it is generally good to put
:: source code and declaration into separate files (.h head filer and
:: .cpp source file). What can I do to template functions/classes?
:: Do I have to put them all into one header file?
::

In theory - no. In practice - Yes.

There is a keyword 'export' in C++, that lets's you write 'export
template...' in the header and then store the implementation in a .cpp
file.

Unfortunately, it is very hard to find a compiler that actually
implements this. :-(

Bo Persson
May 17 '07 #2
Bo Persson wrote:
There is a keyword 'export' in C++, that lets's you write 'export
template...' in the header and then store the implementation in a .cpp
file.

Unfortunately, it is very hard to find a compiler that actually
implements this. :-(
If I understand correctly, the reason why there are still basically
no compilers which support this is because C/C++ compilers have been
for decades designed as a two-part system: The compiler, which creates
object files, and the linker which creates the final executable from
the object files (and possibly libraries). The linker is often very
independent from the compiler and only understands object files.
This makes sense because it allows object files to be created with
different compilers and even different languages, and then they can
be linked together to form one executable. It's not even uncommon
that some precompiled libraries have been created with a completely
different compiler than the one you are using to compile your program.

The difficulty with export templates is that they require the linker
to call the C++ compiler at linking stage. This, of course, creates
heavy dependencies between the linker and the C++ compiler and presents
all kinds of problems which compiler developers seemingly are unwilling
to tackle with at least for now.
May 17 '07 #3
On May 17, 5:16 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Bo Persson wrote:
There is a keyword 'export' in C++, that lets's you write 'export
template...' in the header and then store the implementation in a .cpp
file.
Unfortunately, it is very hard to find a compiler that actually
implements this. :-(
If I understand correctly, the reason why there are still basically
no compilers which support this is because C/C++ compilers have been
for decades designed as a two-part system: The compiler, which creates
object files, and the linker which creates the final executable from
the object files (and possibly libraries). The linker is often very
independent from the compiler and only understands object files.
That may have been true 20 years ago, but I don't think it's
true for any modern C++ compiler. For that matter, even CFront
used a pre-linker (or was it a post-linker), and link time
instantiation of templates. Today, of course, any compiler
worth its salt does intermodule optimization---at link time,
using output from the profiler.
This makes sense because it allows object files to be created with
different compilers and even different languages, and then they can
be linked together to form one executable. It's not even uncommon
that some precompiled libraries have been created with a completely
different compiler than the one you are using to compile your program.
The difficulty with export templates is that they require the linker
to call the C++ compiler at linking stage.
Not necessarily. They do require that information from the
source files with the exported templates be available when the
template is instantiated. How the compiler does this is up to
it.
This, of course, creates heavy dependencies between the linker
and the C++ compiler and presents all kinds of problems which
compiler developers seemingly are unwilling to tackle with at
least for now.
Which is why the one vendor which does fully support export only
sells front-ends, and doesn't require any additional linker
support for it at all?

Since it's earliest days, C++ has required more than the
classical linker provided, and has used various pre- or
post-linker solutions, or linker wrappers, to provide the
additional features. Export doesn't create any additional
problems in that regard.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 17 '07 #4
Bo Persson <bo*@gmb.dkwrote:
Jess wrote:
:: Hello,
::
:: I was told that if I have a template class or template function,
:: then the definitions must be put into the header file where I put
:: the declarations. On the other hand, it is generally good to put
:: source code and declaration into separate files (.h head filer and
:: .cpp source file). What can I do to template functions/classes?
:: Do I have to put them all into one header file?
::

In theory - no. In practice - Yes.

There is a keyword 'export' in C++, that lets's you write 'export
template...' in the header and then store the implementation in a .cpp
file.

Unfortunately, it is very hard to find a compiler that actually
implements this. :-(
True... I think Comeau is the only compiler that officially supports it,
but I think I heard that the Intel compiler has an undocumented switch
for it.

However, the FAQ has a workaround to simulate the export keyword (only
so far as to let you put the definitions in a different file than the
declarations):
http://www.parashift.com/c++-faq-lit...html#faq-35.14

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 18 '07 #5
On May 18, 8:24 am, James Kanze <james.ka...@gmail.comwrote:
On May 17, 5:16 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Bo Persson wrote:
There is a keyword 'export' in C++, that lets's you write 'export
template...' in the header and then store the implementation in a .cpp
file.
Unfortunately, it is very hard to find a compiler that actually
implements this. :-(
If I understand correctly, the reason why there are still basically
no compilers which support this is because C/C++ compilers have been
for decades designed as a two-part system: The compiler, which creates
object files, and the linker which creates the final executable from
the object files (and possibly libraries). The linker is often very
independent from the compiler and only understands object files.

That may have been true 20 years ago, but I don't think it's
true for any modern C++ compiler. For that matter, even CFront
used a pre-linker (or was it a post-linker), and link time
instantiation of templates. Today, of course, any compiler
worth its salt does intermodule optimization---at link time,
using output from the profiler.
This makes sense because it allows object files to be created with
different compilers and even different languages, and then they can
be linked together to form one executable. It's not even uncommon
that some precompiled libraries have been created with a completely
different compiler than the one you are using to compile your program.
The difficulty with export templates is that they require the linker
to call the C++ compiler at linking stage.

Not necessarily. They do require that information from the
source files with the exported templates be available when the
template is instantiated. How the compiler does this is up to
it.
This, of course, creates heavy dependencies between the linker
and the C++ compiler and presents all kinds of problems which
compiler developers seemingly are unwilling to tackle with at
least for now.

Which is why the one vendor which does fully support export only
sells front-ends, and doesn't require any additional linker
support for it at all?

Since it's earliest days, C++ has required more than the
classical linker provided, and has used various pre- or
post-linker solutions, or linker wrappers, to provide the
additional features. Export doesn't create any additional
problems in that regard.

--
James Kanze (Gabi Software) email: james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

I tried to put the declarations in a .h file, with "export" keyword.
When I compiled the code, my compiler said "export" wasn't implemented
and would be ignored. However, the .h file plus the separate .cpp file
actually worked together. If the compiler doesn't implement "export",
how could all these work out?

Jess

May 19 '07 #6
>
I tried to put the declarations in a .h file, with "export" keyword.
When I compiled the code, my compiler said "export" wasn't implemented
and would be ignored. However, the .h file plus the separate .cpp file
actually worked together. If the compiler doesn't implement "export",
how could all these work out?

Jess
Without seeing your code who can say? Maybe you are doing explicit
instantiation (or whatever it's called).

john
May 19 '07 #7
On May 19, 8:50 pm, John Harrison <john_androni...@hotmail.comwrote:
I tried to put the declarations in a .h file, with "export" keyword.
When I compiled the code, my compiler said "export" wasn't implemented
and would be ignored. However, the .h file plus the separate .cpp file
actually worked together. If the compiler doesn't implement "export",
how could all these work out?
Jess

Without seeing your code who can say? Maybe you are doing explicit
instantiation (or whatever it's called).

john

The header file is (test.h):

export template<class Tclass A{
public:
void f() const;
A(T a):x(a){}
private:
T x;
};

The source code is:

#include<iostream>
#include "test.h"

using namespace std;

template<class T>
void A<T>::f() const{
cout << x << endl;
};
int main(){
A<inta(10);
a.f();
return 0;
}

Jess

May 19 '07 #8
Jess wrote:
:: On May 19, 8:50 pm, John Harrison <john_androni...@hotmail.com>
:: wrote:
:::: I tried to put the declarations in a .h file, with "export"
:::: keyword. When I compiled the code, my compiler said "export"
:::: wasn't implemented and would be ignored. However, the .h file
:::: plus the separate .cpp file actually worked together. If the
:::: compiler doesn't implement "export", how could all these work
:::: out?
:::
:::: Jess
:::
::: Without seeing your code who can say? Maybe you are doing explicit
::: instantiation (or whatever it's called).
:::
::: john
::
::
:: The header file is (test.h):
::
:: export template<class Tclass A{
:: public:
:: void f() const;
:: A(T a):x(a){}
:: private:
:: T x;
:: };
::
:: The source code is:
::
:: #include<iostream>
:: #include "test.h"
::
:: using namespace std;
::
:: template<class T>
:: void A<T>::f() const{
:: cout << x << endl;
:: };
::
::
:: int main(){
:: A<inta(10);
:: a.f();
:: return 0;
:: }
::

This works because the implementation of f() is present in the same
file as its use. The effect is the same as if it was put in the header
file.

The including of files is performed by the preprocessor, long before
the compiler starts to look at the code. When you reach that stage, it
appears as one large file with all includes and preprocessor macros
already resolved.

If you were to put f() in a separate A.cpp file, the compiler would
not find it (when ignoring the export keyword).
Bo Persson


May 19 '07 #9
On May 19, 9:27 pm, "Bo Persson" <b...@gmb.dkwrote:
Jess wrote:

:: On May 19, 8:50 pm, John Harrison <john_androni...@hotmail.com>:: wrote:

:::: I tried to put the declarations in a .h file, with "export"
:::: keyword. When I compiled the code, my compiler said "export"
:::: wasn't implemented and would be ignored. However, the .h file
:::: plus the separate .cpp file actually worked together. If the
:::: compiler doesn't implement "export", how could all these work
:::: out?
:::
:::: Jess
:::
::: Without seeing your code who can say? Maybe you are doing explicit
::: instantiation (or whatever it's called).
:::
::: john
::
::
:: The header file is (test.h):
::
:: export template<class Tclass A{
:: public:
:: void f() const;
:: A(T a):x(a){}
:: private:
:: T x;
:: };
::
:: The source code is:
::
:: #include<iostream>
:: #include "test.h"
::
:: using namespace std;
::
:: template<class T>
:: void A<T>::f() const{
:: cout << x << endl;
:: };
::
::
:: int main(){
:: A<inta(10);
:: a.f();
:: return 0;
:: }
::

This works because the implementation of f() is present in the same
file as its use. The effect is the same as if it was put in the header
file.

The including of files is performed by the preprocessor, long before
the compiler starts to look at the code. When you reach that stage, it
appears as one large file with all includes and preprocessor macros
already resolved.

If you were to put f() in a separate A.cpp file, the compiler would
not find it (when ignoring the export keyword).

Bo Persson
I see, thanks a lot. In addition, if I run the program on a compiler
that supports export, shall I export the implemenations (in .cpp) or
the declarations (in .h)? Shall I export each individual functions
like

export template<class TA<T>::f()...
export template<class TA<T>::g()..

or make one "export" declaration at the top of the file?

I remember there's another keyword "extern", which indicates the
current declaration isn't a definition and it implies the definition
is at somewhere else. This sounds similar to "export", what's the
difference between them?

Thanks,
Jess

May 19 '07 #10
On May 19, 12:44 pm, Jess <w...@hotmail.comwrote:
On May 18, 8:24 am, James Kanze <james.ka...@gmail.comwrote:
[...]
I tried to put the declarations in a .h file, with "export" keyword.
When I compiled the code, my compiler said "export" wasn't implemented
and would be ignored. However, the .h file plus the separate .cpp file
actually worked together. If the compiler doesn't implement "export",
how could all these work out?
Many vendors are too lazy to implement export, and don't really
care about their users. There are exceptions. The discussion
wasn't about what current compilers do, it was about why.
Someone purported technical reasons why a compiler wouldn't
implement export; all I'm doing is pointing out that they don't
exist.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 19 '07 #11
Since my compiler doesn't support "export", I'm trying to put .h
and .cpp into two files and then "include" one into the other. I
tried to include the source file (src.cpp) in my header file
(header.h) by putting

#include "src.cpp"

into "header.h" (near the end of the header file).

The problem with this method is that I can't separately compile the
"src.cpp" file, because all the other "include" headers and the
template declarations are in "header.h". I don't think I can put
another set of "include" in "src.cpp", because it would introduce
multiple declarations.

To me, it seems I'd have to put

#include "header.h"

into src.cpp and remove the

#include "src.cpp"

line from "header.h". However, I'm not sure whether this is possible,
because the compiler needs to see the source code in the header file.
How can I solve this problem?

Thanks,
Jess

May 24 '07 #12
Jess <wd***@hotmail.comwrote:
Since my compiler doesn't support "export", I'm trying to put .h
and .cpp into two files and then "include" one into the other. I
tried to include the source file (src.cpp) in my header file
(header.h) by putting

#include "src.cpp"

into "header.h" (near the end of the header file).

The problem with this method is that I can't separately compile the
"src.cpp" file, because all the other "include" headers and the
template declarations are in "header.h". I don't think I can put
another set of "include" in "src.cpp", because it would introduce
multiple declarations.

To me, it seems I'd have to put

#include "header.h"

into src.cpp and remove the

#include "src.cpp"

line from "header.h". However, I'm not sure whether this is possible,
because the compiler needs to see the source code in the header file.
How can I solve this problem?
Make sure you have appropriate include guards in your header files.
Here is a simple working example I created:
// export_template.h
#ifndef EXPORT_TEMPLATE_H
#define EXPORT_TEMPLATE_H

template <typename T>
class Foo {
public:
Foo();
void print() const;
private:
T val_;
};

#ifndef USE_EXPORT_KEYWORD
#include "export_template.cpp"
#endif

#endif
// export_template.cpp
#include "export_template.h"
#include <iostream>

template <typename T>
Foo<T>::Foo() : val_(42) { }

template <typename T>
void Foo<T>::print() const
{
std::cout << "val = " << val_ << '\n';
}
// export_template_use.cpp
#include "export_template.h"
#include <iostream>

int main()
{
Foo<intf;
f.print();
}
Then just compile export_template_use.cpp. No need to compile
export_template.cpp on its own, since my compiler doesn't support export
either.

Also, the FAQ has a technique so that will allow you to use the export
keyword if your compiler supports it in the future (this has been
integrated into the above example):
http://www.parashift.com/c++-faq-lit...html#faq-35.14

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 24 '07 #13
Marcus Kwok <ri******@gehennom.invalidwrote:
Also, the FAQ has a technique so that will allow you to use the export
keyword if your compiler supports it in the future (this has been
integrated into the above example):
http://www.parashift.com/c++-faq-lit...html#faq-35.14
Sorry, I only did part of the job regarding the future export support.
See the FAQ listed above for the rest of the steps you need.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 24 '07 #14
On May 25, 12:40 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
Jess <w...@hotmail.comwrote:
Since my compiler doesn't support "export", I'm trying to put .h
and .cpp into two files and then "include" one into the other. I
tried to include the source file (src.cpp) in my header file
(header.h) by putting
#include "src.cpp"
into "header.h" (near the end of the header file).
The problem with this method is that I can't separately compile the
"src.cpp" file, because all the other "include" headers and the
template declarations are in "header.h". I don't think I can put
another set of "include" in "src.cpp", because it would introduce
multiple declarations.
To me, it seems I'd have to put
#include "header.h"
into src.cpp and remove the
#include "src.cpp"
line from "header.h". However, I'm not sure whether this is possible,
because the compiler needs to see the source code in the header file.
How can I solve this problem?

Make sure you have appropriate include guards in your header files.
Here is a simple working example I created:

// export_template.h
#ifndef EXPORT_TEMPLATE_H
#define EXPORT_TEMPLATE_H

template <typename T>
class Foo {
public:
Foo();
void print() const;
private:
T val_;

};

#ifndef USE_EXPORT_KEYWORD
#include "export_template.cpp"
#endif

#endif

// export_template.cpp
#include "export_template.h"
#include <iostream>

template <typename T>
Foo<T>::Foo() : val_(42) { }

template <typename T>
void Foo<T>::print() const
{
std::cout << "val = " << val_ << '\n';

}

// export_template_use.cpp
#include "export_template.h"
#include <iostream>

int main()
{
Foo<intf;
f.print();

}

Then just compile export_template_use.cpp. No need to compile
export_template.cpp on its own, since my compiler doesn't support export
either.

Also, the FAQ has a technique so that will allow you to use the export
keyword if your compiler supports it in the future (this has been
integrated into the above example):http://www.parashift.com/c++-faq-lit...html#faq-35.14

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

Many thanks for your example, that really helps. :) So I see the key
is have the well-defined guards. I think I can remove

#ifndef USE_EXPORT_KEYWORD

if I don't expect to run my program on any compiler supporting
"export" keyword.
Jess

May 25 '07 #15

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

Similar topics

6
by: Suzanne | last post by:
Hi++, I get a link error when I try to call a template function that is declared in a header file and defined in a non-header file. I do *not* get this link error if I define the template...
12
by: blueblueblue2005 | last post by:
Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error, no matter how I change the include order, please help. here is the files:...
3
by: Juhan Voolaid | last post by:
Hello I'm learning c++ and it is clear to me how to add information to header file about functions. But what about class and class functuons? If i have: 1. main.cpp #include "myHeader.h" ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
18
by: Al | last post by:
I'm still trying to do this but it never worked! In a .cpp file, I write the code, and at the beginning, I write: #ifndef MYLIST_H #define MYLIST_H ....to end: #endif What's wrong with it for...
1
by: Swapnil Kale | last post by:
Hi, I'm working on a Migration project (Forte to JAVA). The forte client had a C++ dll which used to call one more FORTE dll for a complex database calculations. Now all the forte code has...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write all instantiations in Source file of all template methods for various different datatypes that my client might use, Instead, i choose to write implementation...
0
by: anto.anish | last post by:
Hi , Since, i did not want to write instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of template...
1
by: anto.anish | last post by:
Hi , Since, i did not want to write explicit instantiations in Source file of all template methods for various different datatypes that my client might use, i choose to write implementation of...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.