 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
sandor Guest
|
Posted: Sun Mar 27, 2005 2:23 am Post subject: class template specialization question |
|
|
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
|
Posted: Mon Mar 28, 2005 1:35 am Post subject: Re: class template specialization question |
|
|
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 |
|
 |
|
|
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
|
|