Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: Integer promotions, conversions, coercions and templates :( Date: Thu, 09 Jun 2011 10:22:26 +1200 Lines: 42 Message-ID: <95ab12FrnrU6@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net e3RCMAV5jMj6lm7o2JXG7wi9l2U7UznNJ3cmVYlkTGDLHLqEag Cancel-Lock: sha1:SyjPT4NbWe6r6aTLH2Ta1+3bIwc= User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:6451 On 06/ 9/11 10:09 AM, MikeP wrote: > I need help (but nevermind that!) templates and the C++ integer rules are > confusing me! (No occifer, I have not been drinking, I am just dazed and > confused by C++ integer rules!). > > // CheckBit > // Checks the status of individual bits of any unsigned integer type. > // Indexing of bits starts at 1, not 0. > // mask can be any unsigned integer type. > // W is the width of the type T. > // > template > bool CheckBit(T& mask, T n) > { // The assertion should be an exception, I know. > Assert((n>= 1)&& (n<= W), "Bit index out of range.") Missing semicolon. > return ((mask& ((T)1)<< (n-((T)1))) != 0); > } > > I wanted to use the appropriate integer type suffix as a template > parameter to use on the 1's (don't know if I needed to though), but C++ > templates won't allow that so I have the casts-to-T all over the place > but I'm not sure if they are necessary. Why don't you just use const T one(1); return (((mask & one) << (n-one)) != 0); > I made arg n a type T just to > avoid fld and n being different unsigned integer types. fld? > Is CheckBit correct? If not, can it be corrected? Can it be improved? Test it! -- Ian Collins