How to cast a multi-pointer to the simple reference (recursive template cast)
Taken from http://stackoverflow.com/questions/17653266/make-a-templated-structure-to-turn-a-pointer-with-any-count-of-to-work-as-a/17655695#17655695
template <typename T>
struct perform_indirection_t {
typedef T& result;
static result call(T& t) {
return t;
}
};
template <typename T>
struct perform_indirection_t <T*> {
typedef typename perform_indirection_t<T>::result result;
static result call(T* t) {
return perform_indirection_t<T>::call(*t);
}
};
template
<typename T> typename perform_indirection_t<T>::result perform_indirection(T t) {
return perform_indirection_t<T>::call(t);
}
int main()
{
int x = 0;
int* p = &x;
int** pp = &p;
int*** ppp = &pp;
int**** pppp = &ppp;
int***** ppppp = &pppp;
perform_indirection(ppppp) = 1;
std::cout << x;
}
template <typename T>
Comments