A C++ template device to obtain an underlying type

Post by Nico Brailovsky @ 2013-10-15 | Permalink | Leave a comment

What happens when you need to get the underlying data type of a pointer or reference? You can write some crazy metaprogram to do it for you. Like this:

template  struct get_real_type      { typedef T type; };
template  struct get_real_type  { typedef T type; };
template  struct get_real_type  { typedef T type; };
template 
int foo() {
    return get_real_type::type::N;
}
struct Bar {
    static const int N=24;
};
#include 
using namespace std;
int main() {
    cout << foo() << endl;
    cout << foo() << endl;
    cout << foo() << endl;
}

Incidentally, this is also the basis for the implementation of std::remove_reference. Actually you'd be better of using std::remove_reference, for your own sanity.