Created
May 28, 2009 10:07
-
-
Save GabrielL/119208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Determine le type de base d'un pointeur | |
* @{ | |
*/ | |
template <typename T> | |
struct BaseType | |
{ | |
typedef T base_type; | |
}; | |
template <typename T> | |
struct BaseType<T*> | |
{ | |
typedef typename BaseType<T>::base_type base_type; | |
}; | |
/** @} */ | |
/** | |
* La cochonnerie demandee. | |
* @{ | |
*/ | |
template <typename T> | |
typename BaseType<T>::base_type deref(T ptr) | |
{ | |
return ptr; | |
} | |
template <typename T> | |
typename BaseType<T*>::base_type deref(T* ptr) | |
{ | |
return deref(*ptr); | |
} | |
/** @} */ | |
// example | |
#include <iostream> | |
int main() | |
{ | |
int a = 42; | |
int *b = &a; | |
int **c = &b; | |
int ***d = &c; | |
std::cout << deref(d) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment