I want a function which will return value of the pointer, regardless what level of pointer it is. Like it could be single OR double pointer OR Triple pointer OR more, but that function should return the value.
Example:
#include <iostream>
using namespace std;
template <class T>
T func(T arg){
// what to do here or there is some other way to do this?????
}
int main() {
int *p, **pp, ***ppp;
p = new int(5);
pp = &p;
ppp = &pp;
cout << func(p); // should print 5
cout << func(pp); // should print 5
cout << func(ppp); // should print 5
return 0;
}
So, now I want to pass this p, pp, ppp in only one function and it should print or return the value '5'.
Just have one overload that takes any pointer and calls itself dereferenced, and one overload that takes anything:
template <class T>
T func(T arg) {
return arg;
}
template <class T>
auto func(T* arg){
return func(*arg);
}
This is even possible without C++11, just have to write a type trait to do all the dereferencing too:
template <class T>
struct value_type { typedef T type; };
template <class T>
struct value_type<T*> : value_type<T> { };
template <class T>
T func(T arg) {
return arg;
}
template <class T>
typename value_type<T>::type func(T* arg){
return func(*arg);
}