Template partial specialization for __stdcall function pointer

前端 未结 3 1792
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 14:41
typedef bool (*my_function_f)(int, double);
typedef bool (__stdcall *my_function_f2)(int, double);
//            ^^^^^^^^^

template class TFunction;

tem         


        
相关标签:
3条回答
  • 2021-02-15 14:42

    This is because (*) means default calling convention, which is __cdecl.

    template<class R, class T0, class T1>
    class TFunction<R(*)(T0,T1)>
    {
      typedef R (*func_type)(T0,T1);
    };
    

    is actually equal to

    template<class R, class T0, class T1>
    class TFunction<R(__cdecl *)(T0,T1)>
    {
      typedef R (__cdecl *func_type)(T0,T1);
    };
    

    which, of course, will not match an R(__stdcall *)(T0, T1) on Win32 where __stdcall is not ignored. If you want to partially specialize for function pointers then you will need a partial spec for every calling convention you want to accept.

    0 讨论(0)
  • 2021-02-15 14:54

    No, this is by design. The calling convention is very much part of the function declaration, your template function uses the default calling convention. Which is not __stdcall unless you compile with /Gz. The default is /Gd, __cdecl.

    The code compiles when you target x64 because it blissfully has only one calling convention.

    Fix:

    template<class R, class T0, class T1>
    class TFunction<R (__stdcall *)(T0,T1)>
    {
        // etc..
    };
    
    0 讨论(0)
  • 2021-02-15 15:05

    You have not specialized your template for the stdcall case, i.e. you need

    template<class R, class T0, class T1>
    class TFunction<R(__stdcall *)(T0,T1)>
    {
      typedef R (*func_type)(T0,T1);
    };
    

    Not sure about the syntax, untested, but that should be the issue.

    0 讨论(0)
提交回复
热议问题