Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Template pr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 9 Topic 45795 of 47032
Post > Topic >>

Re: Template problems

by Joe Greer <jgreer@[EMAIL PROTECTED] > May 8, 2008 at 08:17 PM

Tim Frink <plfriko@[EMAIL PROTECTED]
> wrote in
news:68emibF2sur53U1@[EMAIL PROTECTED]
 

> 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 ****p a forward declaration style
>> template definition in your header.  That won't work if you are
>> ****pping 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.para****ft.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 
ex****t.  Idealized in the sense that it couldn't be reverse engineered.  
(Even with ex****t, 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 ex****t files aren't that 
hard to reverse).

HTH,
joe
 




 9 Posts in Topic:
Template problems
Tim Frink <plfriko@[EM  2008-05-07 16:04:49 
Re: Template problems
"sk_usenet" <  2008-05-07 09:35:09 
Re: Template problems
Puppet_Sock <puppet_so  2008-05-07 09:39:05 
Re: Template problems
"Victor Bazarov"  2008-05-07 12:58:24 
Re: Template problems
Stephan Ceram <linuxka  2008-05-07 17:28:44 
Re: Template problems
"Victor Bazarov"  2008-05-07 13:41:35 
Re: Template problems
Joe Greer <jgreer@[EMA  2008-05-07 21:08:27 
Re: Template problems
Tim Frink <plfriko@[EM  2008-05-07 21:51:07 
Re: Template problems
Joe Greer <jgreer@[EMA  2008-05-08 20:17:08 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:21:46 CDT 2008.