Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #6516

Re: Integer promotions, conversions, coercions and templates :(

From "MikeP" <mp011011@some.org>
Newsgroups comp.lang.c++
Subject Re: Integer promotions, conversions, coercions and templates :(
Date 2011-06-09 13:44 -0500
Organization A noiseless patient Spider
Message-ID <isr4a9$3rt$1@dont-email.me> (permalink)
References (3 earlier) <95apmqFrnrU10@mid.individual.net> <isqee8$iru$1@dont-email.me> <33f9545c-7aa5-4ee5-ba45-65ce53dcc3c7@f11g2000vbx.googlegroups.com> <isqipn$d22$1@dont-email.me> <38138c33-9794-434a-8ae9-b63a0bbf6b55@gc3g2000vbb.googlegroups.com>

Show all headers | View raw


Öö Tiib wrote:
> On Jun 9, 4:45 pm, "MikeP" <mp011...@some.org> wrote:
>> Tiib wrote:
>>> On Jun 9, 3:30 pm, "MikeP" <mp011...@some.org> wrote:
>>>> Ian Collins wrote:
>>>>> On 06/ 9/11 12:13 PM, MikeP wrote:
>>>>>> Ian Collins wrote:
>>>>>>> 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<typename T, unsigned int W>
>>>>>>>> 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);
>>
>>>>>> Adds one move instruction to get '1' into 'one' (which is in a
>>>>>> register) over the cast-to-T implementation (2 move instructions
>>>>>> for 64-bit T), so I'd keep the ugly version. Using the suffix
>>>>>> would be the way to write CheckBit if it wasn't a template:
>>
>>>>> Poor job on the compiler's part!
>>
>>>>> The function should optimise down to 0 or 1.
>>
>>>> The example is actually an inline method in a class, but even so,
>>>> the compiler didn't inline it. Weird. (My example should have had
>>>> the "inline" keyword on it also). My bad, I'm in debug mode with
>>>> inlining overridden! I'll do the release version and repost later.
>>>> (Still educational though from a learning-assembly and code
>>>> generation perspective).
>>
>>>> It does seem that templates are trumped by a preprocessor macro
>>>> this time, for the macro does not require a change to the
>>>> algorithm. Templates defficient?
>>
>>> You are in debugging mode evaluating performance of
>>> compiler-generated code?
>>
>> No, read the post from the beginning. The post is about the
>> "impedance mismatch" between templates and the C++ type system.
>>
>>> Worthless waste of time. Turn all optimizations that you plan to
>>> use in production code on. Otherwise you get wrong impression what
>>> compiler will really do.
>>
>> Reread what I wrote above! Geez, what an opportunist.
>
> You better retry again. I see no differences in generated code if i
> use inline template function or macro when checking a bit. So you do
> something wrong. Geez what a noob.

Learn to read. English not your 1st language? 

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-08 17:09 -0500
  Re: Integer promotions, conversions, coercions and templates :( Ian Collins <ian-news@hotmail.com> - 2011-06-09 10:22 +1200
    Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-08 17:49 -0500
    Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-08 19:13 -0500
      Re: Integer promotions, conversions, coercions and templates :( Ian Collins <ian-news@hotmail.com> - 2011-06-09 14:32 +1200
        Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-09 07:30 -0500
          Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-09 06:18 -0700
            Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-09 08:45 -0500
              Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-09 06:51 -0700
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-09 13:44 -0500
          Re: Integer promotions, conversions, coercions and templates :( Ian Collins <ian-news@hotmail.com> - 2011-06-10 07:37 +1200
            Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-09 15:11 -0500
              Re: Integer promotions, conversions, coercions and templates :( Ian Collins <ian-news@hotmail.com> - 2011-06-10 09:09 +1200
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-09 17:15 -0500
                Re: Integer promotions, conversions, coercions and templates :( Ian Collins <ian-news@hotmail.com> - 2011-06-10 10:40 +1200
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-10 05:07 -0500
                Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-10 03:14 -0700
                Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-10 03:19 -0700
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-10 06:08 -0500
                Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-10 04:32 -0700
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-10 07:14 -0500
                Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-10 05:27 -0700
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-10 07:35 -0500
                Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-10 06:47 -0700
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-10 06:18 -0500
                Re: Integer promotions, conversions, coercions and templates :( Ian Collins <ian-news@hotmail.com> - 2011-06-11 08:08 +1200
                Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-10 18:41 -0500
    Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-10 08:11 -0500
  Re: Integer promotions, conversions, coercions and templates :( Öö Tiib <ootiib@hot.ee> - 2011-06-09 06:28 -0700
    Re: Integer promotions, conversions, coercions and templates :( "MikeP" <mp011011@some.org> - 2011-06-09 08:50 -0500
  Re: Integer promotions, conversions, coercions and templates :( (Summary so far) "MikeP" <mp011011@some.org> - 2011-06-10 08:40 -0500
    Re: Integer promotions, conversions, coercions and templates :( (Summary so far) Öö Tiib <ootiib@hot.ee> - 2011-06-10 07:14 -0700
      Re: Integer promotions, conversions, coercions and templates :( (Summary so far) "MikeP" <mp011011@some.org> - 2011-06-10 09:17 -0500
    Re: Integer promotions, conversions, coercions and templates :( (Summary so far)(Corrections and more) "MikeP" <mp011011@some.org> - 2011-06-10 09:14 -0500
      Re: Integer promotions, conversions, coercions and templates :( (Results: Generated Code) "MikeP" <mp011011@some.org> - 2011-06-10 12:43 -0500
  Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-14 18:35 -0500
    Re: Integer promotions, conversions, coercions and templates :( (Conclusion) Ian Collins <ian-news@hotmail.com> - 2011-06-15 12:51 +1200
      Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-15 08:45 -0500
    Re: Integer promotions, conversions, coercions and templates :( (Conclusion) Michael DOUBEZ <michael.doubez@free.fr> - 2011-06-15 03:33 -0700
      Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-15 09:17 -0500
        Re: Integer promotions, conversions, coercions and templates :( (Conclusion) Michael DOUBEZ <michael.doubez@free.fr> - 2011-06-16 04:17 -0700
          Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-16 09:45 -0500
            Re: Integer promotions, conversions, coercions and templates :( (Conclusion) Michael DOUBEZ <michael.doubez@free.fr> - 2011-06-17 01:56 -0700
              Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-17 07:58 -0500
                Re: Integer promotions, conversions, coercions and templates :( (Conclusion) Michael DOUBEZ <michael.doubez@free.fr> - 2011-06-17 06:54 -0700
                Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-17 09:19 -0500
              Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-17 08:17 -0500
                Re: Integer promotions, conversions, coercions and templates :( (Conclusion) Michael DOUBEZ <michael.doubez@free.fr> - 2011-06-17 07:33 -0700
                Re: Integer promotions, conversions, coercions and templates :( (Conclusion) "MikeP" <mp011011@some.org> - 2011-06-17 09:49 -0500

csiph-web