Tuesday, April 24, 2012

How to specialize a template member funcion depending on the class' template arguments?

I would like to write this:



template<typename T1, typename T2>
class OK
{
T1 t1;
T2 t2;

public:
template<typename TX> const TX & GetRef() const;
};

template<typename T1,typename T2>
template<>
const T1 & OK<T1,T2>::GetRef<T1>() const { return t1; }


Which VS10 fails to compile.



To check my understanding of template specialization, I tried and compiled this all right:



typedef int  T1;
typedef char T2;
class OK
{
T1 t1;
T2 t2;

public:
template<typename TX> const TX & GetRef() const;
};

template<>
const T1 & OK::GetRef<T1>() const { return t1; }


What am I missing ? Is what I want to do even possible ?





No comments:

Post a Comment