python合法整型常量_【练习】判断类中是否包含一个受保护的静态整形常量val

下面的代码在老的编译器上不work,我用gcc 4.8.2编译没问题

template 

class is_class {

typedef char YES;

typedef int NO;

template  static YES matcher(void (U::*)(void));

template  static NO matcher(...);

public:

static const bool r = sizeof(matcher(0)) == sizeof(YES);

};

template 

class is_public {

typedef char YES;

typedef int NO;

template  static YES (&matcher(int))[1 + 0 * U::val];

template  static NO matcher(...);

public:

static const bool r = sizeof(matcher(0)) == sizeof(YES);

};

template 

class is_public_or_protected : private T {

typedef char YES;

typedef int NO;

template  static YES (&matcher(int))[1 + 0 * is_public_or_protected::val];

template  static NO matcher(...);

public:

static const bool r = sizeof(matcher(0)) == sizeof(YES);

};

template 

struct if_ {

static const bool r = T::r;

};

template 

struct if_ {

static const bool r = false;

};

template 

struct is_protected {

static const bool r = if_::r && !is_public::r, is_public_or_protected >::r;

};

//test case

#include 

static void test(bool r, const char * s) {

printf("%-16s%s\n", r?"PASS":"FAILED", s);

}

#define TEST(x, b) test(is_protected::r == b, #x)

class int0 {};

class int1 { public: static const int val = 0; };

class int2 { protected: static const int val = 0; };

class int3 { private: static const int val = 0; };

template  class T1 : public T {};

template  class T2 : protected T {};

template  class T3 : private T {};

class int4 { protected: static const double val = 0; };

class int5 { protected: static const char val = 0; };

class int6 { protected: static const short val = 0; };

int main() {

TEST(int, false);

TEST(double, false);

TEST(int0, false);

TEST(int1, false);

TEST(int2, true);

TEST(int3, false);

TEST(T1, false);

TEST(T1, true);

TEST(T1, false);

TEST(T2, true);

TEST(T2, true);

TEST(T2, false);

TEST(T3, false);

TEST(T3, false);

TEST(T3, false);

TEST(int4, false);

TEST(int5, true);

TEST(int6, true);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值