Equivalent of template specialization using .NET Generics?

  • Thread starter Thread starter Duncan Smith
  • Start date Start date
D

Duncan Smith

I know that template specializtion is not included in generics, but
was wondering whether or not there is an elegant alternative?

The first function is always called, but I was hoping that for
instances of type RealPoint (ComparePointByFiaNumber<RealPoint)>) the
second function would be called.

To implement different behavious based upon the type, do I have to
have a switch within Equals() that will check the type of T and then
call different functions as needed?

Many thanks,

Duncan

generic<typename T>
ref class ComparePointByFiaNumber : Generic::IEqualityComparer<T>
{
public:

virtual bool Equals( T x, T y )
{
Generic::IEqualityComparer<T>^ equalityComparer =
Generic::EqualityComparer<T>::Default;
return equalityComparer->Equals( x,y );
}

virtual bool Equals( RealPoint^ x, RealPoint^ y )
{
return true;
}
 
I know that template specializtion is not included in generics, but
was wondering whether or not there is an elegant alternative?

Not as pretty as C++ but in the end I went for:

generic<typename T>
ref class ComparePointByFiaNumber : Generic::IEqualityComparer<T>
{
public:

virtual bool Equals( T x, T y )
{
if( x->GetType() == RealPoint::typeid )
{
return safe_cast<RealPoint^>(x)->First ==
safe_cast<RealPoint^>(y)->First;
}

Generic::IEqualityComparer<T>^ equalityComparer =
Generic::EqualityComparer<T>::Default;
return equalityComparer->Equals( x,y );
}

virtual int GetHashCode( T obj )
{
return 0;
}
};
 
Back
Top