Skip to content

Instantly share code, notes, and snippets.

@GabrielL
Created May 28, 2009 10:07
Show Gist options
  • Save GabrielL/119208 to your computer and use it in GitHub Desktop.
Save GabrielL/119208 to your computer and use it in GitHub Desktop.
/**
* 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