Determining whether typedef exists...

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::Data exists, (value == true)
template <class T>
struct HasType_Data{ AVI_DEFINE_HAS_TYPEDEF( Data ); };

#undef AVI_DEFINE_HAS_TYPEDEF


Regards,

Werner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top