DEV Community

海前 王
海前 王

Posted on

萃取

#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;
}
Enter fullscreen mode Exit fullscreen mode

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay