编译期判断类型之间是否可以convert

2014-11-24 03:25:55 · 作者: · 浏览: 0

[cpp]
//T could converted to U
template
class Conversion
{
private:
typedef char Small;
struct Big{ char big[2]; };

static Small _helper_fun(U);
static Big _helper_fun(...);
static T _make_T();
public:
enum {
Exists = (sizeof(_helper_fun(_make_T())) == sizeof(Small)),
Exists2Way = ( Exists && Conversion::Exists),
Same = false
};
};

// partial specialization for "same type"
template
class Conversion
{
public:
enum{
Exists = true,
Exists2Way = true,
Same = true
};
};

// T is subclass of U
template
class IsSubclass
{
public:
enum{
Result = Conversion::Exists
};
};

//T could converted to U
template
class Conversion
{
private:
typedef char Small;
struct Big{ char big[2]; };

static Small _helper_fun(U);
static Big _helper_fun(...);
static T _make_T();
public:
enum {
Exists = (sizeof(_helper_fun(_make_T())) == sizeof(Small)),
Exists2Way = ( Exists && Conversion::Exists),
Same = false
};
};

// partial specialization for "same type"
template
class Conversion
{
public:
enum{
Exists = true,
Exists2Way = true,
Same = true
};
};

// T is subclass of U
template
class IsSubclass
{
public:
enum{
Result = Conversion::Exists
};
};