W
Werner
Hi All,
I've come up with a way to determine whether types have typedefs.
I'll post the code below, but have a couple of questions:
1) Are there better ways of doing this?
2) If you know of libraries providing this sort of thing, kindly
point me to it (I've not seen it being part of boost).
3) Is it worth the effort?
I've probably used ideas from other examples and tweaked it a bit.
Can't remember
where I saw it but some of it is certainly my own - the macro and
SfinaeBase...
//-------------- Lib code ------------------
struct SfinaeBase
{
typedef char No;
typedef struct{ No no_[2]; } Yes;
};
#define AVI_DEFINE_HAS_TYPEDEF( Exp )\
struct Nested : SfinaeBase\
{\
template <typename U>\
static Yes test(typename U::Exp*);\
template <typename>\
static No test(...);\
};\
enum{ value = (sizeof(Nested::template test<T>(0)) == sizeof(typename
Nested::Yes)) }
//--------- Client code --------------
//If T::Header exists, (value == true)
template <class T>
struct HasType_Header{ AVI_DEFINE_HAS_TYPEDEF( Header ); };
//If T::Footer exists, (value == true)
template <class T>
struct HasType_Footer{ AVI_DEFINE_HAS_TYPEDEF( Footer ); };
//If T:ata exists, (value == true)
template <class T>
struct HasType_Data{ AVI_DEFINE_HAS_TYPEDEF( Data ); };
#undef AVI_DEFINE_HAS_TYPEDEF
Regards,
Werner
I've come up with a way to determine whether types have typedefs.
I'll post the code below, but have a couple of questions:
1) Are there better ways of doing this?
2) If you know of libraries providing this sort of thing, kindly
point me to it (I've not seen it being part of boost).
3) Is it worth the effort?
I've probably used ideas from other examples and tweaked it a bit.
Can't remember
where I saw it but some of it is certainly my own - the macro and
SfinaeBase...
//-------------- Lib code ------------------
struct SfinaeBase
{
typedef char No;
typedef struct{ No no_[2]; } Yes;
};
#define AVI_DEFINE_HAS_TYPEDEF( Exp )\
struct Nested : SfinaeBase\
{\
template <typename U>\
static Yes test(typename U::Exp*);\
template <typename>\
static No test(...);\
};\
enum{ value = (sizeof(Nested::template test<T>(0)) == sizeof(typename
Nested::Yes)) }
//--------- Client code --------------
//If T::Header exists, (value == true)
template <class T>
struct HasType_Header{ AVI_DEFINE_HAS_TYPEDEF( Header ); };
//If T::Footer exists, (value == true)
template <class T>
struct HasType_Footer{ AVI_DEFINE_HAS_TYPEDEF( Footer ); };
//If T:ata exists, (value == true)
template <class T>
struct HasType_Data{ AVI_DEFINE_HAS_TYPEDEF( Data ); };
#undef AVI_DEFINE_HAS_TYPEDEF
Regards,
Werner