Mauro 的个人资料Taming the Jungle照片日志列表 工具 帮助

日志


4月17日

Automatic checking numeric range

I was recently involved in a discussion about the lack of “ranged ints” in the C# language. A ranged int is an integer type that would only accept a smaller subrange of all possible integers, say just 1 to 100. This is a discussion dear to me be because I sort of designed “ranged ints” for C++.
 
It all started because I used to develop with Turbo Pascal which *had* “ranged” integer types. You could turn on a compiler option that would automatically validate the range at runtime. I used it as a debugging aid, like an “automatic assert” every time the variable was assigned to. For instance, I would have a “row” variable that must hold values between 1 and 24, so I would declare it to be of a “row type”, a ranged integer of 1 to 24. During development and testing I would get an error if it got assigned something outside of that range. The final “non-checked” version would have this option turned off and have no performance penalties.
 
Then I had to switch to C++ and I missed my “ranged ints”. Since C++ templates allowed not only types but constants as arguments as well, I developed my own “ranged int” classes based on templates. At debug time they would check the range. I then flipped a compiler “define” and they would behave pretty much like a normal int, semantically and performance wise. This was not a popular C++ technique, but I used it left and right, despite that it made for slower compilations and at one point the Borland C++ compiler would choke with so much template usage.
 
I dusted off my C++ skills and wrote such an example as a console Win32 app below – this one is pretty simple (no copy constructor, no necessary operator overloading) and it will actually coerce the value to be within the range instead of issuing a warning, but you can get the idea. Of course, you could do the same with other numeric types such as float.
 
I couldn’t replicate the technique with C#. Maybe if it would accept constants as generics arguments, I could have my “ranged ints” back…
 
Here is the sample:
 
 
#include "stdafx.h"
#define DEBUG
template <int V1, int V2> class RangeInt {
private:
      int Value;
      #ifdef DEBUG
      // Checks range and force into the range if outside
      // An alternative would be to throw an exception or flag the error somehow
      void CheckRange() {
            if (Value < V1) {
                  Value = V1;
            }
            if (Value > V2) {
                  Value = V2;
            }
      }
      #endif
public:
      RangeInt() {
            Value = V1;
      }
      RangeInt(int N) {
            Value = N;
      #ifdef DEBUG
            CheckRange();
      #endif
      }
      operator int() {
            return Value;
      }
};
int _tmain(int argc, _TCHAR* argv[])
{
      RangeInt<1, 100> MyVal;
      MyVal = 0;
      printf("%d\n", MyVal);
      MyVal = 1000;
      printf("%d\n", MyVal);
      return 0;
}
 

评论 (4)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

没有名字发表:
<a href="http://www.rmt-wm.com">RMT-WM</a> <a href="http://rmtvip.jp">rmtvip</a>The baby eagle liked the nest. 「<a href="http://www.rmt-wm.com/games/aion.html">-aion rmt</a>」It was the only world he had ever known. <a href="http://www.rmtvip.jp/game/aion.html">aion RMT</a> It was warm and comfortable, had a great view, 「<a href="http://www.rmt-wm.com/games/0003.html"> リネージュ2 rmt</a> and even better, he had all the food and love and attention that a great mother eagle could provide. <a href="http://www.rmtvip.jp/game/Lineage2_リネージュ2_RMT.html">リネージュ2 RMT</a> Many times each day the mother would swoop down from the sky and land in the nest and feed the baby eagle delicious morsels of food. She was like a god to him, he had no where she came from or how she worked her magic. <a href="http://www.rmt-wm.com/games/A004.html">アラド戦記 rmt</a>The baby eagle was hungry all the time, but the mother eagle would always come just in time with the food and love and attention he craved. The baby eagle grew strong. <a href="http://www.rmtvip.jp/game/アラド戦記_RMT.html">アラド戦記 RMT</a>His vision grew very sharp. He felt good all the time. <a href="http://www.rmt-wm.com/games/0005.html">ff11 rmt</a>Until one day, the mother stopped coming to the nest. <a href="http://www.rmtvip.jp/game/FF11_FFXI_RMT.html">FF11 RMT</a>Thelooked up at the mother and cried "Why did you abandon me? I'm going to die any minute. How could you do faster. He screamed. "I'm dying I'm dying," he cried. He picked up more speed. <a href="http://www.replica-watches-mall.com">Replica Watche</a>
He looked up at his mother. "How could you do this to me?" He looked down. Something strange happens.
http://www.rmt-wm.com/games/A001.html
http://www.rmt-wm.com/games/0018.html
http://www.rmt-wm.com/games/LEGENDofCHUSEN.html
http://www.rmt-wm.com/games/天地大乱rmt.html
http://www.rmtvip.jp/game/信長の野望_RMT.html
http://www.rmtvip.jp/game/新天上碑_RMT.html
http://www.rmtvip.jp/game/RO_ラグナロク_RMT.html
9 月 30 日
duas sugestões:

. O primeiro #ifdef DEBUG é desnecessário, pois métodos de template classes só são gerados quando são usados (uma das grandes vantagens das bibliotecas de templates - WTL / ATL / STL - é que elas só aumentam o tamanho do seu .EXE proporcionalmente ao número de métodos que você usa). Como o segundo #ifdef DEBUG vai excluir a chamada a CheckRange, este não causará geração de código.

. Quando uma situação inesperada ocorrer no seu código, não jogue a sujeira pra debaixo do tapete! Deixe o programa explodir (na versão DEBUG) pra você achar o erro o quanto antes:

explicit RangeInt(int N) {
Value = N;
ASSERT(V1 <= Value && Value <= V2);
}

um abraço,
Léo (Cyber)

4 月 20 日
没有名字发表:
http://www.batterylaptoppower.com/toshiba/pa3451u.php TOSHIBA PA3451U-1BRS PABAS067
http://www.batterylaptoppower.com/toshiba/pa3465u.php Genuine TOSHIBA PA3465U-1BRS Laptop Battery PABAS069
http://www.batterylaptoppower.com/toshiba/pa3536u-1brs-002-battery.php 6.6ah New PA3536U Battery for Toshiba Satellite P200 P205
http://www.batterylaptoppower.com/toshiba/pa3536u-1brs-battery.php New PA3536U Battery for Toshiba Satellite P200 P205
http://www.batterylaptoppower.com/toshiba/pa3534u-1brs-battery.php NEW Genuine Toshiba Battery Satellite PA3534U-1BRS A205
http://www.batterylaptoppower.com/uniwill/un223.php uniwill un223
http://www.batterylaptoppower.com/uniwill/un243.php uniwill BAT-243S1 UN243
http://www.batterylaptoppower.com/uniwill/un255.php uniwill 255-3S4400-G1L1 un255
http://www.batterylaptoppower.com/uniwill/un258.php uniwill Amilo A-1630 A1630 UN258
http://www.batterylaptoppower.com/hp/mini1000.php New 2350mAh battery for Hp Mini 1000 Compaq 700 UMPC
http://www.batterylaptoppower.com/asus/al23-901.php battery for Asus Eee PC 901 1000 1000H 1200 series
http://www.batterylaptoppower.com/asus/al24-1000.php battery for Asus Eee PC 901 1000 1000H 1200 series
http://www.batterylaptoppower.com/asus/al23901.php battery for Asus Eee PC 901 1000 1000H 1200 series
http://www.batterylaptoppower.com/asus/al241000.php battery for Asus Eee PC 901 1000 1000H 1200 series
http://www.batterylaptoppower.com/samsung/nc10.php black Battery For Samsung NC10 ND10 AA-PB8NC6B AA-PB8NC6M
http://www.batterylaptoppower.com/samsung/nc11.php white Battery For Samsung NC10 ND10 AA-PB8NC6B AA-PB8NC6M
http://www.batterylaptoppower.com/samsung/nc12.php blue Battery For Samsung NC10 ND10 AA-PB8NC6B AA-PB8NC6M
http://www.batterylaptoppower.com/samsung/nc13.php black Battery For Samsung NC10 ND10 AA-PB8NC6B AA-PB8NC6M
http://www.batterylaptoppower.com/samsung/nc14.php white Battery For Samsung NC10 ND10 AA-PB8NC6B AA-PB8NC6M
http://www.batterylaptoppower.com/samsung/nc15.php blue Battery For Samsung NC10 ND10 AA-PB8NC6B AA-PB8NC6M
http://www.batterylaptoppower.com/asus/f9.php Asus F9 battery New High Quality Asus F9 battery
http://www.batterylaptoppower.com/asus/s101.php Laptop Battery for Asus Eee PC S101 PCS101 AP22-U1001
http://www.batterylaptoppower.com/asus/u1001.php Laptop Battery for Asus Eee PC S101 PCS101 AP22-U1001
http://www.batterylaptoppower.com/benq/u101.php Battery For BenQ Joybook Lite U101 Mini Botebook
http://www.batterylaptoppower.com/benq/squ-812.php Battery For BenQ Joybook Lite U101 Mini Botebook
http://www.batterylaptoppower.com/clevo/d400.php Battery For Clevo D400 D400S D410 D470W 87-D408S-4C5
http://www.batterylaptoppower.com/samsung/r50.php battery for samsung NP-R50 R55-CV02 R50-1800 M55-T003
http://www.batterylaptoppower.com/hp/db63.php battery for HP 2133 Mini PC HSTNN-DB63 HSTNN-DB64
http://www.batterylaptoppower.com/clevo/6411.php new battery fits Clevo M22BAT-8 2800 D220S D270S M220s
4 月 19 日
没有名字发表:
http://www.batterylaptoppower.com/hp/nc2400-battery.php New Compaq 2510p NC2400 Laptop Extended Battery EH768AA
http://www.batterylaptoppower.com/hp/nc4000.php HP NC4010 DD880A NC4000
http://www.batterylaptoppower.com/hp/nc6000.php battery for Compaq/HP NX5000 NC6000 NC8000 DG105A 4.4A
http://www.batterylaptoppower.com/hp/zt1000.php New Battery For HP Pavilion ZT1000 F2299A F3172B F3172A
http://www.batterylaptoppower.com/ibm/02k7018.php IBM THINKPAD 600 600A 600D 600E 600X 02K7018 BATTERY
http://www.batterylaptoppower.com/ibm/02k7055.php Laptop Battery 02K6928 02K7055 for IBM Thinkpad R32 R40
http://www.batterylaptoppower.com/ibm/02k7072.php Laptop Battery IBM 02K7072 02K7034 ThinkPad T30
http://www.batterylaptoppower.com/ibm/41n5666.php NEW Battery for IBM/Lenovo Thinkpad t60 92P1141
http://www.batterylaptoppower.com/ibm/92p1000.php New 4.4Ah battery fits IBM Thinkpad X40 X41 92P1002
http://www.batterylaptoppower.com/mitac/8050-battery.php Battery For Packard Bell EasyNote BP-8050(P) 40006487
http://www.batterylaptoppower.com/mitac/8375.php MITAC 8375 8575 8575A 8575P LAPTOP BATTERY black
http://www.batterylaptoppower.com/mitac/8375-2.php MITAC 8375 8575 8575A 8575P LAPTOP BATTERY grey
http://www.batterylaptoppower.com/mitac/advent8089.php Battery For Bell BP-8089 BP-8089X BP-8389 BP-8089P
http://www.batterylaptoppower.com/mitac/bp-8599.php Packard Bell Easynote F5280 Laptop Battery MYBAT9528 (CBI1010A)
http://www.batterylaptoppower.com/nec/op-570-75303.php battery for NEC PC-VP-WP25 OP-570-75303 75301
http://www.batterylaptoppower.com/nec/op-570-75901.php nec pc-vp-wp44 op-570-75901
http://www.batterylaptoppower.com/sony/pcga-bp2sa-battery.php Battery For Sony PCGA-BP2S PCGA-BP2SA VAIO PCG-SR17 NEW black
http://www.batterylaptoppower.com/sony/pcga-bp2t.php battery fits sony PCGA-BP2T PCGA-BP3T PCG-TR1/B PCG-TR2
http://www.batterylaptoppower.com/sony/pcga-bp2v.php laptop Battery for Sony PCGA-BP2V
http://www.batterylaptoppower.com/sony/pcg-gr100.php Original SONY pcga-bp2e vgp-bp2ea pcg-gr100 Battery
http://www.batterylaptoppower.com/sony/vgp-bps2a.php LAPTOP BATTERY SONY VAIO VGP-BPS2 VGP-BPS2B VGP-BPS2A black
http://www.batterylaptoppower.com/sony/vgp-bps2a-002-battery.php 7.2ah LAPTOP BATTERY SONY VAIO VGP-BPS2 VGP-BPS2B VGP-BPS2A black
http://www.batterylaptoppower.com/toshiba/pa2487ur.php Laptop Battery PA2487UR PA2487U for Toshiba 4000 1800
http://www.batterylaptoppower.com/toshiba/pa3062u-1bar-battery.php New Genuine Toshiba Tecra 8200 Battery PA3062U-1BAR
http://www.batterylaptoppower.com/toshiba/pa3176u-1bas.php Battery For Toshiba Portege M200 M205 PA3128U PA3191U black
http://www.batterylaptoppower.com/toshiba/pa3176u-1bas-2.php Battery For Toshiba Portege M200 M205 PA3128U PA3191U black
http://www.batterylaptoppower.com/toshiba/pa3209u-1brs.php Battery for Toshiba Satellite 1100 1110 PA3209U-1BRS
http://www.batterylaptoppower.com/toshiba/pa3210u-battery.php NEW Toshiba Satellite 1115 4500mAh Battery PA3210U
http://www.batterylaptoppower.com/toshiba/pa3399u.php battery fits TOSHIBA PA3399U-1BAS PA3399U-1BRS
http://www.batterylaptoppower.com/toshiba/pa3399u-1bas.php Battery for Toshiba PA3399U-1BRS PA3399U-2BAS 1BAS
http://www.batterylaptoppower.com/toshiba/pa3356u-1brs.php Toshiba PA3356U-3BAS PA3356U-3BRS PA3456U-1BRS Battery
http://www.batterylaptoppower.com/toshiba/pa3356u-1brs-002-battery.php 8.8ah Battery TOSHIBA PA3356U-1BAS PA3356U-1BRS PA3356U-2BRS
http://www.batterylaptoppower.com/toshiba/pa3383.php Battery For PA3383 TOSHIBA Satellite PA3383U-1BRS 12cells
http://www.batterylaptoppower.com/toshiba/pa3420u.php toshiba pa3420u-1brs l10 l15 l20
http://www.batterylaptoppower.com/toshiba/pa3421u-1brs.php Battery for Toshiba M30X M35X M40X PA3395U-1BRS PA3421U
http://www.batterylaptoppower.com/toshiba/pa3431u-1brs.php TOSHIBA SATELLITE M65 BATTERY PA3431U-BRS PABAS068
4 月 19 日

引用通告

此日志的引用通告 URL 是:
http://maurosjungle.spaces.live.com/blog/cns!F3CEB0849B03B6CC!651.trak
引用此项的网络日志