萃取

#include <iostream>
#include <memory>
using namespace std;

/*先定义一些tag*/
struct A {};
struct B : A {};

/*假设有一个未知类*/
template <class AorB>
struct unknown_class {
typedef AorB return_type;
};

/*特性萃取器*/
template <class unknown_…


This content originally appeared on DEV Community and was authored by 海前 王

#include <iostream>
#include <memory>
using namespace std;

/*先定义一些tag*/
struct A {};
struct B : A {};

/*假设有一个未知类*/
template <class AorB>
struct unknown_class {
    typedef AorB return_type;
};

/*特性萃取器*/
template <class unknown_class>
struct unknown_class_traits {
    typedef typename unknown_class::return_type return_type;
};

/*特性萃取器 —— 针对原生指针*/
template <class T>
struct unknown_class_traits<T*> {
    typedef T return_type;
};

/*特性萃取其 —— 针对指向常数*/
template <class T>
struct unknown_class_traits<const T*> {
    typedef const T return_type;
};

/*特性萃取器 —— 针对std::unique_ptr*/
template <class T>
struct unknown_class_traits<std::unique_ptr<T>> {
    typedef T return_type;
};

/*特性萃取器 —— 针对std::shared_ptr*/
template <class T>
struct unknown_class_traits<std::shared_ptr<T>> {
    typedef T return_type;
};

/*决定使用哪一个类型*/
template <class unknown_class>
inline typename unknown_class_traits<unknown_class>::return_type
return_type(unknown_class) {
    typedef typename unknown_class_traits<unknown_class>::return_type RT;
    return RT();
}

template <class T>
typename unknown_class_traits<T>::return_type __func(T, A) {
    cout << "use A flag" << endl;
    return typename unknown_class_traits<T>::return_type();
}

template <class T>
typename unknown_class_traits<T>::return_type
__func(T, B) {
    cout << "use B flag" << endl;
    return typename unknown_class_traits<T>::return_type();
}

template <class T, class U>
typename unknown_class_traits<T>::return_type
__func(T, U) {
    cout << "use origin ptr" << endl;
    return typename unknown_class_traits<T>::return_type();
}

template <class T, class U>
typename unknown_class_traits<T>::return_type
__func(T, std::unique_ptr<U>) {
    cout << "use unique_ptr flag" << endl;
    return typename unknown_class_traits<T>::return_type();
}

template <class T, class U>
typename unknown_class_traits<T>::return_type
__func(T, std::shared_ptr<U>) {
    cout << "use shared_ptr flag" << endl;
    return typename unknown_class_traits<T>::return_type();
}

template <class unknown_class>
typename unknown_class_traits<unknown_class>::return_type
func(unknown_class u) {
    typedef typename unknown_class_traits<unknown_class>::return_type return_type;
    return __func(u, return_type());
}

int main() {
    unknown_class<B> b;
    unknown_class<A> a;
    int value = 1;
    int* p = &value;
    shared_ptr<int> p1 ( p);
    shared_ptr<int> p2 (p1);

    A v1 = func(a);
    A v2 = func(b);
    B v4 = func(b);

    int v3 = func(p);
//    int v5 = func();
    //shared_ptr<int > (func(p2));
    func(p2);
    cout << "d"<<endl;
    const char* f = "cc";
    func(f);
    const char* v = f;
    func(v);
    int j[10] = { 0 };
    int* k = j;
    func(k);

    char ch = getchar();
    return 0;
}


This content originally appeared on DEV Community and was authored by 海前 王


Print Share Comment Cite Upload Translate Updates
APA

海前 王 | Sciencx (2024-08-27T01:35:08+00:00) 萃取. Retrieved from https://www.scien.cx/2024/08/27/%e8%90%83%e5%8f%96/

MLA
" » 萃取." 海前 王 | Sciencx - Tuesday August 27, 2024, https://www.scien.cx/2024/08/27/%e8%90%83%e5%8f%96/
HARVARD
海前 王 | Sciencx Tuesday August 27, 2024 » 萃取., viewed ,<https://www.scien.cx/2024/08/27/%e8%90%83%e5%8f%96/>
VANCOUVER
海前 王 | Sciencx - » 萃取. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/27/%e8%90%83%e5%8f%96/
CHICAGO
" » 萃取." 海前 王 | Sciencx - Accessed . https://www.scien.cx/2024/08/27/%e8%90%83%e5%8f%96/
IEEE
" » 萃取." 海前 王 | Sciencx [Online]. Available: https://www.scien.cx/2024/08/27/%e8%90%83%e5%8f%96/. [Accessed: ]
rf:citation
» 萃取 | 海前 王 | Sciencx | https://www.scien.cx/2024/08/27/%e8%90%83%e5%8f%96/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.