mardi 1 septembre 2015

Regarding the C++ array type

I asked a similar question before but my next question will be in reference to the following code

#include <iostream>
using namespace std;

template <typename T>
struct which_type {
    static inline string type() {return "General type";}
};

template <typename T>
struct which_type<T*> {
    static inline string type() {return "T*";}
};

template <typename T>
struct which_type<T**> {
    static inline string type() {return "T**";}
};

template <typename T, int N>
struct which_type<T (&) [N]> {
    static inline string type() {return "T (&) [N]";}
};

template <typename T, int N>
struct which_type<T (*) [N]> {
    static inline string type() {return "T (*) [N]";}
};

template <typename T, size_t N>
struct which_type<T[N]> {
    static inline string type() {return "T[N]";}
};

template <typename T>
struct which_type<T[]> {
    static inline string type() {return "T[]";}
};

void which_type_is_array(int a[]) {
    cout << which_type<decltype(a)>::type() << endl;
}


extern int e[];

int main() {

    int a[1];
    int *b;
    int* c[1];
    int (&d) [1] = a;
    cout << which_type<decltype(a)>::type() << endl;
    cout << which_type<decltype(b)>::type() << endl;
    cout << which_type<decltype(c)>::type() << endl;
    cout << which_type<decltype(d)>::type() << endl;
    cout << which_type<decltype(e)>::type() << endl;
    which_type_is_array(a);

    // cout << what_number<2>::what() << endl;


    // int (*ret_int) (int);
    // ret_int = &foo;
    // cout << ret_int(1) << endl;


    return 0;
}

In which cases does the call to which_type<>::type() get resolved to the partially specialized case of the struct taking a T[]? The case of the extern int does but not of the one in which_type_is_array(). Why?

Thanks!

Aucun commentaire:

Enregistrer un commentaire