Re: Template problems
Tim Frink <plfriko@yahoo.de> wrote in
news:68emibF2sur53U1@mid.individual.net:
Hi,
One other possibility (of limited scope) is to explicitly instantiate
your template with the common types you are likely to want and
compile that into a lib and only ship a forward declaration style
template definition in your header. That won't work if you are
shipping a template that is supposed to work with a user defined
class though.
What you mean is probably the second suggested solution in
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.15
(add template class Foo<int>; in Foo.cpp), right?
This example is describing a template class, however I'm using
a template function
template< typename T >
void func( bool( T::*f)(void) );
Can this trick with the forward declaration be also applied to
template functions? If so, what do I have to insert in my example file
a.cc? template class A<B>; won't work.
I believe so. The following seems to work for me (I make no guarantees
about it being what you want to do and things like include guards and
doing something useful is left as an exercise), however:
::: t-f.h ::: The public file
template<class T>
bool func(bool (T::*f)(), T * p);
class AlwaysFails
{
public:
bool f();
};
class AlwaysWorks
{
public:
bool f();
};
::: t1.cpp ::: The private implementation distributed as .o or .obj
#include "t-f.h"
bool AlwaysFails::f()
{
return false;
}
bool AlwaysWorks::f()
{
return true;
}
template<class T> bool func( bool (T::*f)(), T * p)
{
return (p->*f)();
}
template bool func( bool (AlwaysFails::*f)(), AlwaysFails * p);
template bool func( bool (AlwaysWorks::*f)(), AlwaysWorks * p);
::: t.cpp ::: The client app
#include <iostream>
#include <ostream>
#include "t-f.h"
int main()
{
AlwaysFails af;
AlwaysWorks aw;
std::cout << "AlwaysFail = " << func(&AlwaysFails::f, &af) <<
std::endl;
std::cout << "AlwaysWork = " << func(&AlwaysWorks::f, &aw) <<
std::endl;
}
This solution seems to be a sort of a hack because I must know in
advance what template types will be use for 'T'. Can I add different
forward declarations (obviously this must be adjusted to my template
functions) like
template class A<B>;
template class A<C>;
If so it is still better than using the concrete type of class
for "T".
Yes, its definitely a work-around. You really want an idealized form of
export. Idealized in the sense that it couldn't be reverse engineered.
(Even with export, the source needs to be destributed, but it's in some
internal form, An ambitious soul could decompile it into the original
source, so its still a less than ideal solution, but compilers aren't
really into the security business so AFAIK the export files aren't that
hard to reverse).
HTH,
joe