Andy Little wrote:
> kwikius wrote:
>> On Jun 13, 10:42 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
>>> kwikius wrote:
>
>>>> You could probably set this up fairly simply to use compile time
>>>> recursion. Use a functor templated on N which calls N-1 and
>>>> specialize it for zero (or other way up ) case etc...
>>> Yes one could do that. But why?
>>
>> Only one way to find out....
>>
>> <... usual inertia elided ...>
>
> Apologies That was a bit fierce, however using compile time loop
> unrolling is a useful technique, and the OP was asking why it wasnt
> didnt seem possible. I was merely pointing out that it is possible.
> In some cases the compiler may do loop unrolling for you, but doing
> it explicitly using compile time recursion is one less uncertainty/
> layer of complexity for the optimiser.
>
> Interestingly, looking at std::bitset, Its kind of a weird hybrid
> because the length is fixed but many ops are runtime.
>
> As an example one could add a bitset.flip<Pos>() function to
> accompany bitset.flip(pos). The first version being again easier
> to optimise and all the information re the op is available at
> compile time unlike the runtime parameter, and very usefully, the
> range can be checked at compile time too, so there is no need of a
> runtime check and possible exception, unlike appears to be the case
> in the runtime flip (which throws an exception on out of range).
> The same applies to many (probably nearly all) ops in bitset AFAICS.
>
Any decent optimizer can do that anyway, when pos is a compile time
constant. That saves you from modifying the application code when that
condition changes.
Bo Persson
|