Computing the level of indirection for pointer types
Posted by: rmn on: 25/10/2009
I’ve finally had a little time to advance with reading Alexandrescu’s book on Modern C++ design, and I’m absolutely blown away by the sheer awesomeness of its ideas. I highly recommend getting this book!
I would like to show just a little bit of what we can do with template specialization when it comes down to meta programming.
Suppose we wanted to know how many levels of indirection does our type actually have. What I mean in “levels of indirection” is basically how many times do we need to de-reference the type in order to get to the real underlying type. Here’s how it’s possible to achieve just that, in compile time, using template specializations:
template <typename T>
struct LevelOfIndirection {
enum { result = 0 };
typedef T pointedToType;
};
template <typename T>
struct LevelOfIndirection<T*> {
enum { result = LevelOfIndirection<T>::result + 1 };
typedef typename LevelOfIndirection<T>::pointedToType pointedToType;
};
#include <iostream>
int main () {
std::cout <<
"int**: " << LevelOfIndirection<int**>::result <<
", int: " << LevelOfIndirection<int>::result <<
std::endl;
// LevelOfIndirection<int**>::pointedToType is int!
}
http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315
The second specialization matches pointer types better and therefore gets selected by the compiler as long as T represents a pointer type. So by using it, we’re able to make all the calculations we’re interested in.
This is just something I came up with to demonstrate the power in yielding template specializations for meta programming. There are many other interesting techniques I plan to introduce in the near future.
Advertisement
Like this:
Be the first to like this post.
26/10/2009 at 22:27
Can you think of case where I would need to do such a thing?
29/10/2009 at 23:21
Well, I could come up with some bizzare scenario, but the point I want to make is a little different..
This code is presented as an original example (perhaps too original?) for this kind of technique/approach. The very same technique can be applied in many real-life situations, and this simple example only strives to demonstrate the basic concept – in order to get people familiar with this kind of thinking.