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


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

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

From "MikeP" <mp011011@some.org>
Newsgroups comp.lang.c++
Subject Re: Integer promotions, conversions, coercions and templates :( (Conclusion)
Date 2011-06-15 09:17 -0500
Organization A noiseless patient Spider
Message-ID <itaeuo$ljn$2@dont-email.me> (permalink)
References <isorvh$sre$1@dont-email.me> <it8r83$4bv$1@dont-email.me> <93956ef8-85ef-4805-81a9-794694d2d5d5@u19g2000vbi.googlegroups.com>

Show all headers | View raw


Michael DOUBEZ wrote:
> On 15 juin, 01:35, "MikeP" <mp011...@some.org> wrote:
>> I opted for a preprocessor macro approach (I used the "super macro"
>> technique) to implement the actual code alluded to by the example. I
>> feel that aberrating the algorithm with C++-isms to make up for the
>> deficiencies in the language is an inferior solution to the macro
>> approach.
>
> AFAIS many solutions have been proposed.

All rejected, in the end though.

> Clearly stating the desired
> result would have helped.

Surely it was clear that what desired was a "template" to generate a very 
finite set of bit field classes. Efficiency counts for such. If elegance 
of implementation surfaced as another requirement (indeed, that made the 
macro approach trump the template approaches offered, in the end), so be 
it. I think that this is a good case study.

>
>> Remember, that the example was not the actual code (the actual
>> code is a class and the example is indicative of one of the methods
>> of that class), and that the example suffers even more ills caused
>> by the inferior/deficient/incorrect C++ type system. Note that the
>> point was to generate 4 different definitions of the class
>> (function, in the example) for use and not meant to be a template or
>> macro to be used directly.
>
> You can use - not yet standard - static_assert() and - tr1 standard -
> is_unsigned to limit the range of template parameter type:
>
> template <typename T, unsigned int W = numeric_limit<T>::digits >
> bool CheckBit(T& mask, unsigned n)
> {
>  static_assert( is_integral<T>::value && is_unsigned<T>::value );
>  // assert n in range
>  const T mask_n = T(1) << (n-1ui32);
>  return mask & mask_n != 0;
> }

How is that more clear than using zero and the width of the unsigned 
integer type in the assertion?

(Aside: I have a problem with succumbing to "T(1)"  instead of "1uiXX" 
just to get type consistency, that's why I like the macro better).

>
> And use your super macro for generating the
> inline bool CheckBit8(uin8& mask, unsigned n)
> {
>  return CheckBit<uint8,8>(mask,n)
> }
> ...
>
> Better to put as little as possible in the macro.

I have used the "generate unique names with a macro for macro-based 
templates so much though, that it is a well-tested and accepted 
technique.

>
>> Here's a stab at a macro (non "super macro") for the example function
>> which won't work because the compiler won't be able to decide on
>> which overload to use:
>
> Well, it will fail on signed type since you are using non-const
> parameter by reference.

It should be const for the example.

> But, it shouldn't fail on unsigned type.

Oh? You don't think that overloading a function on only all of the 
unsigned integer types will be a problem?

>
> [snip - code]
>
>> Conclusions
>>
>> The C++ template system cannot fully replace preprocessor techniques.
>
> Well, it cannot generate new class/function name,

That is DOES do, under the covers.

> cannot generate a
> switch/case and cannot transform a parameter into its string
> equivalent.

It looks like an oversight (problem unaddressed) to design a generic type 
mechanism and not address literals within that mechanism. I think that 
pretty much characterizes the issue in the use case scenario in this 
thread.

>
> AFAIK whose are the only cases where MACRO are really needed.
>
>> The C++ type system sucks.
>
> It is a security. Some prefer to drive without seatbelt.

It's baggage from C.

>
>> The above 2 things make C++ tedious to use and difficult to learn. 

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