Search code examples
compilationc++-cli.net-4.5.2

C++/CLI Compilation error with generic delegate


I'm currently doing some work with C++/CLI, and am encountering a strange compilation error with the following code:

private ref class LinqHelper abstract sealed
{
public:
    generic<typename T, typename U> static Func<T, U>^ Cast()
    {
        return gcnew Func<T, U>(&LinqHelper::Cast);
    }

private:
    generic<typename T, typename U> static U Cast(T val)
    {
        return safe_cast<U>(val);
    }
};

error C3352: 'Cast' : the specified function does not match the delegate type 'U (T)'

To be more specific, the problematic line of code is:

return gcnew Func<T, U>(&LinqHelper::Cast);

I find myself at a loss to explain this error. The Cast static member function that I am passing to the Func delegate constructor has the required signature, i.e. U Cast(T val).

Could anyone help shed some light on this for me please?

If it is of any help: i am working with VS 2015 and the C++/CLI project is referencing the .NET framework v4.5.2.

Thank you


Solution

  • The compiler is not happy about having to infer the type arguments for the Cast method. Lousy error message, not uncommon in C++/CLI. Fix:

    return gcnew Func<T, U>(&LinqHelper::Cast<T, U>);