Equivalent of template specialization using .NET Generics?

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;
}
 
D

Duncan Smith

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;
}
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top