|
Boost : |
From: Mats Nilsson (mats.nilsson_at_[hidden])
Date: 2002-04-05 04:17:10
Sorry if this list is wrong for this topic. Feel free to direct me
elsewhere.
I'm trying to make use of a Loki::Typelist to represent a collection of
metadata for a data marshaller.
The types that I place in the Typelist are typically template
instantiations.
I have found myself in a position where I need to locate an entry in a list
by its template. I've come up with something that doesn't work. It always
finds a match in the first element in the typelist.
Given:
#include <loki/Typelist.h>
template <class T> struct Foo {
typedef T Result;
};
template <class T> struct Bar {
typedef T Result;
};
struct DefaultFallbackType {
typedef char Result;
};
typedef TYPELIST_2(Bar<int>, Foo<float>) L;
I want to search L for the first type that is a instantiation of Foo:
typedef LocateTemplateType<L, Foo, DefaultFallbackType>::Result::Result
XXX;
I have tried to define this template like this, but it is not correct in
some way. With the code below and gcc 2.95.3-5 cygwin, XXX evaluates to a
Bar<int> instead of a Foo<float>.
Can anyone spot the error?
template <template <class> class Template,
class OtherType>
struct IsTemplateSpecialization {
enum {value = false };
};
template <template <class> class Template,
class X>
struct IsTemplateSpecialization<Template, Template<X> > {
enum { value = true };
};
template <class TList,
template <class> class Template,
class Default> struct LocateTemplateType;
template <template <class> class Template,
class Default>
struct LocateTemplateType<Loki::NullType, Template, Default> {
typedef Default Result;
};
template <class Head, class Tail, template <class> class Template, class
Default>
struct LocateTemplateType<Loki::Typelist<Head, Tail>, Template, Default> {
typedef Loki::Select<IsTemplateSpecialization<Template, Head>::value,
Head,
typename LocateTemplateType<Tail,
Template,
Default
>::Result
>::Result Result;
};
Thanks,
Mats
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk