|
Boost : |
From: Christoph Ludwig (cludwig_at_[hidden])
Date: 2004-08-14 13:28:07
On Sat, Aug 14, 2004 at 10:57:36AM -0700, Paul Mensonides wrote:
> I suppose another way to do it would be:
>
> template<class T> struct id {
> typedef T type;
> };
>
> template<class> struct pack;
> template<class T> struct pack<void (id<T>)> : id<T> { };
>
> MY_MACRO(pack<void (id<void>)>::type)
> MY_MACRO(pack<void (id<MyTemplate<int, int>[10]>)>::type)
>
> But an even better way is to do the unpacking in the macro and clean up the
> syntax:
>
> struct pack;
>
> template<class T> struct lock {
> typedef T type;
> };
>
> template<class T> struct unpack : lock<T> { };
> template<> struct unpack<pack (void)> : lock<void> { };
> template<class T> struct unpack<pack (T)> : lock<T> { };
> template<class T> struct unpack<pack (lock<T>)> : lock<T> { };
>
> #define MY_MACRO(t) ... unpack<t>::type ...
>
> MY_MACRO( int )
> MY_MACRO( pack (std::pair<int, int>) )
> MY_MACRO( pack (lock<int[10]>) )
Wow. I am always surprised what you can achieve with some template
metaprogramming. And I consider your example particularly elegant
because a user can ignore the machinery unless the naive approach
fails. Very neat!
Thanks for writing it down!
Regards
Christoph
-- http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/cludwig.html LiDIA: http://www.informatik.tu-darmstadt.de/TI/LiDIA/Welcome.html
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk