| View previous topic :: View next topic |
| Author |
Message |
B. Perlman Guest
|
Posted: Wed Jan 26, 2005 7:31 pm Post subject: What is the syntax for putting the definition of a member fu |
|
|
Can someone tell me the syntax for putting the definition of a member
function of a template class in a separate implementation file? I
tried Stroustrup and couldn't work it out. For example:
MyClass.h
template<class T> class MyClass
{
public:
explicit MyClass(...) {... }
// this works:
// ~MyClass() {
// /* do something */
// }
~MyClass();
};
MyClass.cpp
MyClass::~MyClass() { // WHAT IS THE SYNTAX FOR THIS LINE???
/* do something */
}
Thank you.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ian McCulloch Guest
|
Posted: Wed Jan 26, 2005 8:53 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
B. Perlman wrote:
| Quote: | Can someone tell me the syntax for putting the definition of a member
function of a template class in a separate implementation file? I
tried Stroustrup and couldn't work it out. For example:
MyClass.h
template<class T> class MyClass
{
public:
explicit MyClass(...) {... }
// this works:
// ~MyClass() {
// /* do something */
// }
~MyClass();
};
MyClass.cpp
MyClass::~MyClass() { // WHAT IS THE SYNTAX FOR THIS LINE???
/* do something */
}
|
Well, the syntax would be
template <class T>
MyClass<T>::~MyClass()
{
// do something
}
but remember that this definition must be available to all users of the
template, so this needs to either go in the header itself, or in a file
that is included by the header. Trying to compile a separate .cpp file
won't work.
HTH,
Ian McCulloch
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Randy Guest
|
Posted: Thu Jan 27, 2005 1:07 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
B. Perlman wrote:
| Quote: | Can someone tell me the syntax for putting the definition of a member
function of a template class in a separate implementation file? I
tried Stroustrup and couldn't work it out. For example:
MyClass.h
template<class T> class MyClass
{
public:
explicit MyClass(...) {... }
// this works:
// ~MyClass() {
// /* do something */
// }
~MyClass();
};
MyClass.cpp
MyClass::~MyClass() { // WHAT IS THE SYNTAX FOR THIS LINE???
/* do something */
}
Thank you.
|
template <class T>
MyClass<T>::~MyClass()
{
}
I believe that is correct.
Randy.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Jan 27, 2005 1:08 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
B. Perlman wrote:
| Quote: | Can someone tell me the syntax for putting the definition of a member
function of a template class in a separate implementation file? I
tried Stroustrup and couldn't work it out. For example:
MyClass.h
template<class T> class MyClass
{
public:
explicit MyClass(...) {... }
// this works:
// ~MyClass() {
// /* do something */
// }
~MyClass();
};
MyClass.cpp
MyClass::~MyClass() { // WHAT IS THE SYNTAX FOR THIS LINE???
/* do something */
}
|
template<class T> MyClass<T>::~MyClass() {
But why do you want it in a separate file? Unless you include that file
during compilation of a unit that attempts to instantiate your template
and causes the d-tor to be called, you are bound to have errors when the
linker won't find the definition for the destructor...
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Thu Jan 27, 2005 8:50 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
Victor Bazarov wrote:
| Quote: | B. Perlman wrote:
Can someone tell me the syntax for putting the definition of a member
function of a template class in a separate implementation file?
[snip]
template<class T> MyClass<T>::~MyClass() {
But why do you want it in a separate file? Unless you include that file
during compilation of a unit that attempts to instantiate your template
and causes the d-tor to be called, you are bound to have errors when the
linker won't find the definition for the destructor...
|
For aesthetic reasons, it may be nice to look at a class declaration
that is free of implementation details; the details would be #included
below the declaration in the same header file.
Another possibility is that you're building a library, and providing
only a fixed set of MyClass<T> instantiations. By hiding the
implementation files, you can insure that library users are restricted
to the instantiations that you have built [and tested]. This would
require that your build process ensures that the instantiations are
complete, and that the user environment does not interact negatively
with these restrictions.
Also, with such configurations, you can arrange for tools like doxygen
to run on a smaller amount of source code than you would need for a full
build. It may even be possible to "optimize" compilation by reducing
the amount of source code passing through the compiler (not as simple
as optimizing the doxygen pass, obviously).
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Randy Guest
|
Posted: Thu Jan 27, 2005 8:53 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
Victor Bazarov wrote:
[snip]
| Quote: | But why do you want it in a separate file? Unless you include that
file
during compilation of a unit that attempts to instantiate your
template
and causes the d-tor to be called, you are bound to have errors when
the
linker won't find the definition for the destructor...
V
|
This is not, or at least was not always, completely true. In many
cases, such as with MSVC++, it is true that the template definition
must be seen at compile time along with the template declaration.
However, it is also the case that some other compilers, older versions
of HP C++ on HP Unix come to mind, rely on a different convention where
for some template declared in TemplateX.h the compiler expects that the
implementation will be found in TemplateX.cpp in the same directory as
TemplateX.h. At compile time the compiler merely looks at the template
declarations and keeps some sort of record about which templates need
to be instantiated. Then as part of the linking process the compiler
is invoked again to compile exactly those instantiations that are
required.
I haven't worked with a compiler lately that supports this model, but
it used to fairly common in the Unix world.
The usual way to support both models was to have a compile time define
that controlled whether or not the template header file also included
the template implementation file. For example, near the end of
TemplateX.h you might see somthing like:
#if defined(IncludeTemplateImplementation)
#include "TemplateX.cpp"
#endif
I'm sure this must look familiar to at least a few old C++ hands.
Randy.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
B. Perlman Guest
|
Posted: Thu Jan 27, 2005 9:13 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
Ian McCulloch wrote:
| Quote: | but remember that this definition must be available to all users of
the
template, so this needs to either go in the header itself, or in a
file
that is included by the header. Trying to compile a separate .cpp
file
won't work.
|
You can't just link the implementation file, as with a normal class?
Why is that -- is it because the compiler has to generate the code for
the class when it is used?
Thank you.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
|
| Back to top |
|
 |
GianGuz Guest
|
Posted: Thu Jan 27, 2005 9:21 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
The 'explicit' qualifiers is implementation depend.
I only know EGCS that fully support it.
Anyway, two of the widely used tecniques to manage
templates declarations and definitions belongs to a
combination of the Inclusion and Explicit
Instantiation Model [Vandervoorde, 2003].
Here is an example:
test.hpp
#ifndef TEST_HPP
#define TEST_HPP
template<class T> class Test {
public:
Test() { }
inline const T testObject();
const int sizeOfTest();
~Test () { }
};
//Note that inline must always be in the first
//.hpp file to avoid conflicts with the
//EXPLICIT_INSTANTIATION model
template<typename T> const T Test<T>::testObject() {
return T();
}
#endif
testdef.hpp
#ifndef TESTDEF_HPP
#define TESTDEF_HPP
#include "test.hpp"
template<typename T> const int Test<T>::sizeOfTest() {
return sizeof(testObject());
}
#endif
test_inst.cpp
#include "testdef.hpp"
#include <string>
template Test<string>;
template<> const int Test<string>::sizeOfTest() {
static string s="";
return sizeof(s);
}
Consider now a main() that wants to use the Test class:
testmain.cpp
#ifdef INCLUSION_MODEL
#include "testdef.hpp"
#else //EXPLICIT INSTANTIATION MODEL
#include "test.hpp"
#endif
#include <string>
#include <iostream>
int main() {
Test<std::string> firstTest;
std::cout << firstTest.sizeOfTest() << std::endl;
}
Compiling with the INCLUSION_MODEL flag that
source works without any linking. The implementations
are available in the same translation unit. Compiling
with the EXPLICITIC INSTANTIATION MODEL needs linking to work.
Test
to a separate translation unit (test_inst.o). So that
object file must be linked togheter with the main source.
Note that only the Test<string>::sizeOfTest()
implementation is provided. Declaring in main.cpp
firstTest as a Test<int> for example will result
in a linker error.
Gianguglielmo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Fri Jan 28, 2005 2:04 am Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
B. Perlman wrote:
| Quote: | You can't just link the implementation file, as with a normal class?
Why is that -- is it because the compiler has to generate the code for
the class when it is used?
|
A template instantiation involves the template parameters.
In their absence, a compiler cannot generate an object file
in the conventional sense from a template definition, because
it does not have all the data available.
Suppose you write
template <typename T> void increment(T &t) { ++t; }
How should this be compiled into a conventional linkable object
file? What would it contain? Even if you could come up with a
sharing implementation, there would probably be so much overhead
that it would be unusable.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Fri Jan 28, 2005 11:24 am Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
Hyman Rosen <hyrosen (AT) mail (DOT) com> writes:
| Quote: | B. Perlman wrote:
You can't just link the implementation file, as with a normal class?
Why is that -- is it because the compiler has to generate the code for
the class when it is used?
A template instantiation involves the template parameters.
In their absence, a compiler cannot generate an object file
in the conventional sense from a template definition, because
it does not have all the data available.
Suppose you write
template <typename T> void increment(T &t) { ++t; }
How should this be compiled into a conventional linkable object
file? What would it contain? Even if you could come up with a
sharing implementation, there would probably be so much overhead
that it would be unusable.
|
Not really unusable. Haskell works exactly that way. It often isn't
as efficient as C++ but it's easy to imagine a specializing (different
sense of the word) compiler that would use link-time inlining to
eliminate the indirect call overhead in inner loops.
So, it can be done, even for C++. But all C++ compilers I know of
happen to implement a model that begins with generating separate
object code for each distinct template specialization.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andy Guest
|
Posted: Sat Jan 29, 2005 9:37 am Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
This might be an OT. Just remember the export keyword. I guess it's not
all that a favourite with ppl. Does that have any relevance to this
topic?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
GianGuz Guest
|
Posted: Mon Jan 31, 2005 8:29 pm Post subject: Re: What is the syntax for putting the definition of a membe |
|
|
In part it is an error of mine because I read 'export'
instead of 'explicit' in MyClass constructor. But the
various template inclusion and instantiation models
I presented does not seem to me OT at all. They simply answer
in a generalized way to Pearlman's case study/problem.
Gianguglielmo
Andy wrote:
| Quote: | This might be an OT. Just remember the export keyword. I guess it's
not
all that a favourite with ppl. Does that have any relevance to this
topic?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|