C++类模板的重载

由于在看 QT5.15.2中的 connect方法中的 typedef QtPrivate::FunctionPointer<func1> SignalType;</func1>使用以及, QtPrivate::FunctionPointer的声明。而产生的疑问,因此有了这篇内容。 QtPrivate::FunctionPointer的声明如下:


template<typename Func> struct FunctionPointer { enum {ArgumentCount = -1, IsPointerToMemberFunction = false}; };

template<class Obj, typename Ret, typename... Args> struct FunctionPointer<Ret (Obj::*) (Args...)>
    {
        typedef Obj Object;
        typedef List<Args...>  Arguments;
        typedef Ret ReturnType;
        typedef Ret (Obj::*Function) (Args...);
        enum {ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = true};
        template <typename SignalArgs, typename R>
        static void call(Function f, Obj *o, void **arg) {
            FunctorCall<typename Indexes<ArgumentCount>::Value, SignalArgs, R, Function>::call(f, o, arg);
        }
    };

QtPrivate::FunctionPointer的声明总共有七个,这里不再一一粘贴,看到这里不知诸位会不会产生以下几个疑问:

我这里使用的环境是 VS2019编写的代码, C++ 14的编译标准

到这里我想最初的那三个问题,小伙伴们应该已经知道答案了吧。问题如下:

根据 typedef List<args...> Arguments;</args...>衍生的验证, List模板类型的声明如下:

template <typename...> struct List { int primary; };
template <typename Head, typename... Tail> struct List<Head, Tail...> { typedef Head Car; typedef List<Tail...> Cdr; int ts; };

2.1 其他的几种写法

特化写法如下:

template <typename A, typename B> struct templateSpecialization {};
template <typename A> struct templateSpecialization<A, int> {};
template <> struct templateSpecialization<char, int> {};

#include
using namespace std;
class MyClass
{
public:
    MyClass() = default;
    ~MyClass() = default;
    void test(int,int) { return; }
};

template <typename A, typename B> struct templateSpecialization {};
template <typename A> struct templateSpecialization<A, int> {};
template <> struct templateSpecialization<char, int> {};

template <typename...> struct List { int primary; };
template <typename Head, typename... Tail> struct List<Head, Tail...> { typedef Head Car; typedef List<Tail...> Cdr; int ts; };

template <typename A, typename...> struct List2 { int primary; };
template <typename A, typename B,typename... Tail> struct List2<A, B,Tail...> { int abtail; };
template <typename A> struct List2<A> { int A; };
template <typename A, typename B> struct List2<A,B> { int AB; };
template <typename A, typename B, typename C> struct List2<A,B,C> { int ABC; };

template <typename A> struct Test { int primary; };
template <class A, typename... B> struct Test<A(*) (B...)> { int testAmultiB; };

template<class Obj> struct FunctionPointer {};
template<class Obj, typename Ret, typename... Args> struct FunctionPointer<Ret(Obj::*) (Args...)>
{
    typedef Obj Object;
    enum { ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = true };
    typedef List<Args...>  Arguments;
};

template <typename Func1, typename Func2>
static inline void connect(const typename FunctionPointer<Func1>::Object* sender, Func1 signal,
    const typename FunctionPointer<Func2>::Object* receiver, Func2 slot)
{
    const type_info& nInfo = typeid(Func1);
    cout << nInfo.name() << " | " << nInfo.raw_name() << " | " << nInfo.hash_code() << endl;
    const type_info& inInfo = typeid(FunctionPointer<Func1>::Arguments::Car);
    cout << inInfo.name() << " | " << inInfo.raw_name() << " | " << inInfo.hash_code() << endl;

    cout << FunctionPointer<Func1>::ArgumentCount << endl;

}

int main()
{
    typedef void (*ptr)();
    Test<ptr> test1;
    test1.testAmultiB;

    FunctionPointer<void(MyClass::*)()> fpTest;

    List<int,string> list1;
    list1.ts = 0;

    List<int> list2;
    list2.ts = 0;

    List<> list3;
    list3.primary = 0;

    List<int, string,int> list4;
    list4.ts = 0;

    List2<int> list21;
    list21.A = 0;

    List2<int,int> list22;
    list22.AB = 0;

    List2<int,int,int> list23;
    list23.ABC;

    MyClass a;
    connect(&a, &MyClass::test, &a, &MyClass::test);

}

Original: https://blog.csdn.net/weixin_41111116/article/details/127821651
Author: 愤怒的小黄鸭
Title: C++类模板的重载

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/658851/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球