Using gernerics Predicate<T> in C++/CLI

A

adebaene

Hello all,

Has everyone tried to use the functions taking a Predicate in Generics
container in C++/CLI?

Say I have a List<MyClass^>^ my_array, and I want to call RemoveAll on
it. How would you do this?

The most natural approach would be :

MyClass^ elem_to_be_removed = //......;
Predicate<MyClass^>^ equality_predicate=gcnew
Predicate<MyClass^>(handler, &MyClass::Equals); //1
my_array->RemoveAll(equality_predicate);

This is already klunky and heavy-syntax, but it also doesn't compile!
At line //1, the compiler complains that the Equals method doesn't have
the right signature (which is true, because this is really the
Object::Equals method here - but I would have hoped that some kind of
"parameter type covariance" would have worked here).

I also have tried using the IEquatable interface, but it doesn't work
either. How are you supposed to use all those functions with predicates
in C++/CLI? Do you need to hand-write a specific function just for
handle-equality checking for *each* type ?

It really is a pity, but it looks like one more time the designers
thought only about C# (where you've got anonymous delegates) when they
designed the whole thing. I hope the STL.NETwill do better!

Also, concerning all this stuff, it there any plan to support generics
delegates in C++/CLI?

Arnaud
MVP - VC
 

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