【问题标题】:Variadic templated delegate in C++CX for use in non managed variadic template classC++CX 中的可变参数模板化委托,用于非托管可变参数模板类
【发布时间】:2015-11-27 02:18:25
【问题描述】:

您可以将模板应用于 C++CX 中的引用类,也可以将它们应用于普通类。显然,您不能将模板应用于委托并引用它,以便编译器将根据需要多次实现它。您可以尝试在 ref 类中定义模板委托,但是您不能将其公开甚至内部,这违背了目的。但是,您可以在本机类中引用委托实例。例如。您可以接受 C++CX 委托作为本机类的参数,并且可以将其作为参考,但您不能在类本身中定义类型。

有人知道如何让可变参数模板原生类使用并持有与模板的返回类型和可变参数参数匹配的 WinRT/UWP 委托吗?

此模板将完全编译为windows运行时组件,只需要在内部使用。

【问题讨论】:

    标签: templates delegates interop uwp c++-cx


    【解决方案1】:

    我发现无法将可变参数模板与委托一起使用。但是,我确实发现您可以简单地在 ref 类上调用标准成员函数指针 - 我错误地有些“假设”无法做到这一点,因为我不相信您可以在没有编组的情况下在 C++/CLI 中做到这一点(尽管语法相似,但我仍然通过我的“编程”了解到这些是完全不同的平台。)

    如果有人需要 C++CX 的可变参数“委托”,欢迎您使用此代码(并不是说它特别复杂)...无论如何,您不能在运行时组件中导出泛型/模板,所以它不像您'将会有不合集类型的问题。

    template<typename MemberTyp, typename ReturnTyp, typename ...ParamTyps>
    private ref class UWPDelegate
    {       
    public:
        typedef ReturnTyp(MemberTyp::*myFPTyp)(ParamTyps...);
    private:        
        myFPTyp fp;
        MemberTyp ^instance;
    
    internal:
        UWPDelegate(MemberTyp^ targetObject, myFPTyp targetMethod);
    
        ReturnTyp operator()(ParamTyps... args);
    
    };
    
    template<typename MemberTyp, typename ReturnTyp, typename ...ParamTyps>
    inline UWPDelegate<MemberTyp, ReturnTyp, ParamTyps...>::UWPDelegate(MemberTyp ^ targetObject, myFPTyp targetMethod)
    {
        fp = targetMethod;
        instance = targetObject;
    }
    
    template<typename MemberTyp, typename ReturnTyp, typename ...ParamTyps>
    inline ReturnTyp UWPDelegate<MemberTyp, ReturnTyp, ParamTyps...>::operator()(ParamTyps ...args)
    {
        return (instance->*fp)(args...);
    }
    

    然后您可以在本机类模板中使用它来接受可变委托参数。

    【讨论】:

      猜你喜欢
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 2016-12-01
      • 2023-03-07
      • 2021-10-01
      相关资源
      最近更新 更多