加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C中编译时整数变量的最小值和最大值

发布时间:2020-12-16 06:47:33 所属栏目:百科 来源:网络整理
导读:有一种简单,干净的方法可以在编译时确定某些变量(此时是未知的)整数变量或类型的最大值和最小值吗?使用模板? 例如: // Somewhere in a large project is:typedef unsigned long XType;typedef char YType;// ...// Somewhere else XType a; YType b; LONG
有一种简单,干净的方法可以在编译时确定某些变量(此时是未知的)整数变量或类型的最大值和最小值吗?使用模板?

例如:

// Somewhere in a large project is:
typedef unsigned long XType;
typedef char YType;
// ...

// Somewhere else
   XType a;
   YType b;
   LONGLONG c,d,e,f;
   c = MinOfType(a); // Same as c = 0;
   d = MaxOfType(a); // Same as d = 0xffffffff;
   e = MinOfType(b); // Same as e = -128;
   f = MaxOfType(b); // Same as f = 127;
// Also would be nice
   e = MinOfType(YType); // Same as e = -128; // Using the typename directly
// Or perhaps
   e = MinOfType<YType>(); // Same as e = -128; // Using the typename directly

解决方法

查看 boost integer_traits.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读