C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

class template specialization question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
sandor
Guest





PostPosted: Sun Mar 27, 2005 2:23 am    Post subject: class template specialization question Reply with quote



Hi,

I have a template class defined in a holy unchangeable library. I would
like to specialize some methods of this class for every type T, where
Pred<T>::r == 1
In pseudo-code:
--- holy_library.h ----
template <typename T>
class Example
{
void f()
{
print( "This should be called for general T types" );
}
};

--- client.h ----
#include "library.h"

template <typename T>
class Pred
{
enum { r = notRelevantForThisQuestion };
};

template < ??? >
void Example < ??? >::f()
{
print( "This should be called for T types where Pred<T>::r == 1" );
};

Is there any way to do this in client.h? No changes are allowed to
holy_library.h

thanks,
Sandor


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Maxim Yegorushkin
Guest





PostPosted: Mon Mar 28, 2005 1:35 am    Post subject: Re: class template specialization question Reply with quote



On 26 Mar 2005 21:23:31 -0500, sandor <sandor.hojtsy (AT) gmail (DOT) com> wrote:

Quote:
Hi,

I have a template class defined in a holy unchangeable library. I would
like to specialize some methods of this class for every type T, where
Pred<T>::r == 1
In pseudo-code:
--- holy_library.h ----
template <typename T
class Example
{
void f()
{
print( "This should be called for general T types" );
}
};

--- client.h ----
#include "library.h"

template class Pred
{
enum { r = notRelevantForThisQuestion };
};

template < ???
void Example < ??? >::f()
{
print( "This should be called for T types where Pred<T>::r == 1" );
};

Is there any way to do this in client.h? No changes are allowed to
holy_library.h

Just add another level of abstraction. Do not use Example directly, proxy
it, for example:

template <typename T>
struct Pred
{
enum { r = 0 };
};

template <typename T>
struct Pred<T*>
{
enum { r = 1 };
};


template<class T, int C = Pred
struct ExampleProxy : Example<T>
{
};

template<class T>
struct ExampleProxy<T, 1>
{
void f(); // here is your f()
};

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.