SaintJob 2.0
SaintJob 2.0

Reputation: 574

Can the IComparer class in C++/CLI have a template argument list?

Using Visual Studio 2015 with this simple code I'm getting the message: "class System::Collections::IComparer may not have a template argument list" when trying to define the MarketOrder_SortMaxPriceToMinPrice class.

In C# I can create the class/method with template arguments but in C++ I can only do it with generic Object^ handles.

using namespace System;
using namespace System::Collections;

ref class MarketOrder : public IComparable<MarketOrder^> //Works fine
{
    public:
    virtual int CompareTo(MarketOrder^ other);
};

ref class MarketOrder_SortMaxPriceToMinPrice : IComparer<MarketOrder^> //Not allowed
{
    public:
    virtual int Compare(MarketOrder^ x, MarketOrder^ y);
};

Is this a C++/CLI limitation or am I doing something wrong?

The following code works for me:

ref class MarketOrder_SortMaxPriceToMinPrice : IComparer
{
    public:
    virtual int Compare(Object^ x, Object^ y);
};

Upvotes: 0

Views: 1097

Answers (1)

David Yaw
David Yaw

Reputation: 27864

There are two IComparer classes, one generic and one non-generic. You need to reference the one in namespace System::Collections::Generic.

Also, the recommendation is to subclass Comparer<T>, rather than directly implementing the interface. This way, you automatically get the non-generic overload.

Upvotes: 2

Related Questions