对类型施加逻辑限制 [英] Putting logical limitations on types

查看:52
本文介绍了对类型施加逻辑限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以定义类型,对类型包含的数据有限制吗,

而不仅仅是它在内存中的大小?


例如:一个bound_int只能在一定范围内,所以

不要像这样一个绑定检查功能:


int in_range(int i,int min,int max){

---- if(i< min)

-------- return min;

---- if(i> max)

-------- return max; < br $>
}


或者是prime_number类型,依此类推。


谢谢,

Ido。

Hi,
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?

For example: a bound_int which can only be in a certain range, so
don''t i don''t have to have a bound checking function like this one:

int in_range(int i, int min, int max){
----if(i<min)
--------return min;
----if(i>max)
--------return max;
}

Or a prime_number type, and so on.

Thanks,
Ido.

推荐答案

Ido Yehieli写道:
Ido Yehieli wrote:



我可以定义类型,对类型包含的数据有限制吗,

而不仅仅是它在内存中的大小?


For例如:一个只能在一定范围内的bound_int,所以

我们不需要有这样的绑定检查功能:


int in_range(int i,int min,int max){

---- if(i< min)

--------返回min;

---- if(i> max)

--- -----返回最大值;

}


或prime_number类型,依此类推。
Hi,
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?

For example: a bound_int which can only be in a certain range, so
don''t i don''t have to have a bound checking function like this one:

int in_range(int i, int min, int max){
----if(i<min)
--------return min;
----if(i>max)
--------return max;
}

Or a prime_number type, and so on.



类型T的变量可以存储T'范围内的任何值,

不受限制。您无法将

值限制为子范围或子集:


int hour; / * 0到23,请* /

小时= 42; / *什么都没有阻止这个* /


int odd; / *仅限奇数,请* /

odd = 42; / *什么都没有阻止这个* /


-

Eric Sosman
es ***** @ ieee-dot-org.inva lid

A variable of type T can store any value in T''s range,
without restriction. There is no way you can limit the
values to a subrange or subset:

int hour; /* 0 through 23, please */
hour = 42; /* nothing prevents this */

int odd; /* odd numbers only, please */
odd = 42; /* nothing prevents this */

--
Eric Sosman
es*****@ieee-dot-org.invalid


文章< 92 *** ******** @ e23g2000prf。 googlegroups.com>,

Ido Yehieli< Id ********* @ gmail.comwrote:
In article <92**********************************@e23g2000prf. googlegroups.com>,
Ido Yehieli <Id*********@gmail.comwrote:

我可以吗定义类型,对类型包含的数据有限制,而不仅仅是它在内存中的大小?
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?


>例如:一个只能在某个范围内的bound_int,所以
不要没有具有绑定检查功能
>For example: a bound_int which can only be in a certain range, so
don''t i don''t have to have a bound checking function



否,不在C.


注意:C枚举(enum)暗示你正在寻找什么
for,但它们不会阻止存储其他值。

实际上它们只是给出了积分常数的名称,而不是

比控制数据类型。

-

所以你找到了你的解决方案

你最后的贡献是什么?

- Supertramp(Fool's Overture)

No, not in C.

Note: C enumerations ("enum") are suggestive of what you are looking
for, but they do NOT prevent other values from being stored.
Effectively they just give names for integral constants, rather
than controlling the data type.
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool''s Overture)


Ido Yehieli< Id ********* @ gmail.comwrites:
Ido Yehieli <Id*********@gmail.comwrites:

我可以定义类型,对类型包含的数据有限制,

而不仅仅是它在内存中的大小?
can I define types with a limitation on the data the type contain,
rather than just the size it will have in memory?



不在C.

-

char a [] =" \ n .CJacehknorstu" ;; int putchar(int); int main(void){unsigned long b []

= {0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},* p
= b,i = 24; for(; p + =!* p; * p / = 4)switch(0 [p]& 3)case 0:{return 0; for(p--; i - ; i - )case +

2:{i ++; if(i)break; else default:continue; if(0)case 1:putchar(a [i& 15]);打破;}}}

Not in C.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}


这篇关于对类型施加逻辑限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆